On Friday, March 11, 2016 at 10:57:12 AM UTC-8, Alexander Popov wrote: > > Hi! > > I have one_to_one association > <http://sequel.jeremyevans.net/rdoc/files/doc/association_basics_rdoc.html#label-one_to_many+and+one_to_one> > > But when I try > > Album.new(artist: @artist) # or .create; @artist exists > > I get: > > 2016-03-11 19:17:24 - Sequel::Error - object #<Album @values={}> does not > have a primary key: > /home/alex/.gem/ruby/2.3.0/gems/sequel-4.32.0/lib/sequel/model/associations.rb:2431:in > > `set_one_to_one_associated_object' > /home/alex/.gem/ruby/2.3.0/gems/sequel-4.32.0/lib/sequel/model/associations.rb:2043:in > > `block in def_one_to_many' > /home/alex/.gem/ruby/2.3.0/gems/sequel-4.32.0/lib/sequel/model/base.rb:2149:in > > `block in set_restricted' > /home/alex/.gem/ruby/2.3.0/gems/sequel-4.32.0/lib/sequel/model/base.rb:2146:in > > `each' > /home/alex/.gem/ruby/2.3.0/gems/sequel-4.32.0/lib/sequel/model/base.rb:2146:in > > `set_restricted' > /home/alex/.gem/ruby/2.3.0/gems/sequel-4.32.0/lib/sequel/model/base.rb:1575:in > > `set' > /home/alex/.gem/ruby/2.3.0/gems/sequel-4.32.0/lib/sequel/model/base.rb:2102:in > > `initialize_set' > /home/alex/.gem/ruby/2.3.0/gems/sequel-4.32.0/lib/sequel/model/base.rb:1177:in > > `initialize' > /home/alex/.gem/ruby/2.3.0/gems/sequel-4.32.0/lib/sequel/model/base.rb:150:in > `new' > > > But when: > > Album.new(artist_id: @artist.id) # or .create, again > > Everything is OK. > > What's wrong? How I can initialize new child object associated with parent? >
You should probably be using many_to_one instead of one_to_one, would be my guess. Album.many_to_one :artist means albums(artist_id) REFERENCES artists. Album.one_to_one :artist means artists(album_id) REFERENCES albums. Based on your second example using artist_id, it appears you want many_to_one. Thanks, Jeremy -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/sequel-talk. For more options, visit https://groups.google.com/d/optout.
