ForeignKey get_db_prep_lookup doesn't work for custom primary key
|Reported by:||Ivan Sagalaev||Owned by:||Malcolm Tredinnick|
|Component:||Database layer (models, ORM)||Version:||master|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
If a parent model has a user-defined field as a primary key:
class Parent(models.Model): user_id = CutomUserField(primary_key=True) class Child(models.Model): parent = models.ForeignKey(Parent)
then this query will fail:
p = Parent(...) Child.objects.filter(parent=p)
This happens because get_db_prep_lookup doesn't get called on p.user_id and its value (which is a custom user class) gets passed as an sql parameter into db.
P.S. A bug is very practical: we have custom non-model user objects instead of contrib.auth.models.User and can't reference them from models with custom UserField because many queries using those models would break.
Change History (15)
comment:1 Changed 8 years ago by
|Patch needs improvement:||unset|