rom-factory icon indicating copy to clipboard operation
rom-factory copied to clipboard

It is not possible to overwrite an association with nil.

Open wuarmin opened this issue 1 year ago • 0 comments

Describe the bug

It is not possible to overwrite an association with nil.

class Units < ROM::Relation[:sql]
  schema(:units, infer: true) do
    associations do
      has_many :users
    end
  end
end

class Users < ROM::Relation[:sql]
  schema(:users, infer: true) do
    associations do
      belongs_to :unit
    end
  end
end
Factory.define(:user) do |f|
  f.id { fake(:number, :number, digits: 3) }
  f.name { fake(:name, :name) }
  f.association(:unit)
end

Following overwrite of the association unit fails:

Factory[:user, id: 1, unit: nil] 
NoMethodError:
       undefined method `fetch' for nil:NilClass
     
               child.merge(fk => parent.fetch(pk))
# /usr/local/bundle/gems/rom-core-5.2.5/lib/rom/associations/many_to_one.rb:45:in `associate'
# /usr/local/bundle/bundler/gems/rom-factory-94c8d02faf68/lib/rom/factory/attributes/association.rb:57:in `call'

Expected behavior

the expected behavior should be a resulting user-db-entry holding a unit_id of NULL and the returned ruby-object should be look like this:

{ id: 1, name: "Michael Knight", unit: nil }

Workaround

Factory[:user, id: 1, unit: { id: nil }] 

WDYT? How should we fix this? Thanks and best regards

wuarmin avatar Sep 06 '22 07:09 wuarmin