You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importpeeweeclassA(peewee.Model):
passclassB(peewee.Model):
a=peewee.ForeignKeyField(A, null=True)
b=B(a=A())
# works as expected:b.a# <A: None># unexpected:b.a=Noneb.a# <A: None>
Expected behavior: when None is assigned to the ForeignKeyField, the field value is None.
Actual behavior: assigning None to the ForeignKeyField has no effect.
How to reset a foreign key to None?
The text was updated successfully, but these errors were encountered:
This was happening because peewee only cleared the related object reference (the A instance on B) if the actual value of the FK changed. Since it was NULL to start with (e.g. A.id is None), peewee did not register that there was anything to do. In practice I don't believe this would lead to any inconsistency in the database, but I can see how it might be confusing.
Thank you for the answer! I think I understand now: setting b.a to None was interpreted as setting b.a.id to None, which is already true. In my application, I distinguish between "the A record is not created yet" and "there is no A record related to this B record". So you're right, it was confusing. I found a workaround, though, by implementing A.__bool__.
Expected behavior: when
None
is assigned to the ForeignKeyField, the field value is None.Actual behavior: assigning
None
to the ForeignKeyField has no effect.How to reset a foreign key to None?
The text was updated successfully, but these errors were encountered: