ForeignKey filters use the wrong field to prepare values for database
|Reported by:||russellm||Owned by:||carljm|
|Component:||Database layer (models, ORM)||Version:||1.0|
|Cc:||kmishler@…, CarstenF, EroSennin||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
There are a couple of edge cases where filters on foreign keys can fail, as a result of a deep-seated problem with the way get_db_prep_value() is used.
If Model A has a foreign key pointing to B, a query of A.objects.filter(b=b_instance) will use the primary key on A to prepare the value for the database.
If both models have the same primary key type, this poses no problem.
- the two fields have a different get_db_prep_value() implementation (usually caused by having primary keys that are of substantially different types), or
- the foreign key specifies a to_field argument
then the value prepared for database lookup will be wrong.
Attached patch describes regression tests for this problem.
Change History (22)
Changed 6 years ago by russellm
comment:1 Changed 6 years ago by Alex
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Triage Stage changed from Unreviewed to Accepted