That isn't the exact code I'm using, I was just trying to use a simple 
example with something one to one. 

For a more realistic example I have a `user account` model that has a one 
to one association with another model to track the current corresponding 
instance of that model. What it currently corresponds to can be updated 
multiple times (not super frequently but maybe every few months). Is there 
any way to force the other types of associations to update on model updates?

On Friday, June 14, 2019 at 3:26:08 PM UTC-4, Jeremy Evans wrote:
>
> 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/f12cb03f-62b7-4cc7-85fe-72d57bd420b3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to