rom-rb / rom-factory

Data generator with support for persistence backends
MIT License
83 stars 42 forks source link

It is not possible to overwrite an association with nil. #76

Closed wuarmin closed 4 months ago

wuarmin commented 2 years ago

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 commented 4 months ago

Has been fixed in 2ca92214a9409dc4fa9c6a514890983c49bf3142. Thank you @solnic!