On Friday, June 14, 2019 at 12:18:57 PM UTC-7, Jake wrote: > > I'm not sure if this is an intended feature or a bug but I couldn't find > any documentation about this. > > ex. > class Person < Sequel::Model > one_to_one :pet, key: :pet_type, primary_key: :pet_type > end > > class Pet < Sequel::Model > end > > Pet.create(pet_type: 'Cat') > Pet.create(pet_type: 'Dog') > > person = Person.create(pet_type: 'Cat') > > puts person.pet_type # outputs: Cat > puts person.pet.pet_type # outputs: Cat > > person.update(pet_type: 'Dog') > > puts person.pet_type # outputs: Dog > puts person.pet.pet_type # outputs: Cat ??? > > > I'm guessing it's some kind of caching issue but this seems kind of > counterintuitive. This has caused issues if I check if an association > exists on an instance, then update the foreign key for that association and > try to access the new/updated association, I get the original association, > not the new/updated one. The same issue happens if the foreign key is > null/the original association doesn't exist and then gets updated. >
I'm guessing you want many_to_one instead of one_to_one for your association. That's only a guess because you didn't post your schema. For many_to_one, Sequel will automatically clear the related associations if the foreign key changes. That doesn't happen for other associations, as the assumption is you will not change the primary key of the object/association, as doing so would break referential integrity. 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. To view this discussion on the web visit https://groups.google.com/d/msgid/sequel-talk/1c4f14aa-b0c5-42fb-b485-47cc1442c050%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
