Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#27594 closed Bug (fixed)

Reverse one to one related object populated from incorrect query columns

Reported by: Daniel Hillier Owned by: Daniel Hillier
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: onetoone reverse
Cc: daniel.hillier@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Daniel Hillier)

Extracting data for the related object from a queryset with a self referential one to one field gets the wrong data (incorrect columns) from the query when using a select_related with the reverse field.

The incorrect columns are introduced by the sqlcompiler mistaking the reverse select_related path as a part of a multiple concrete model inheritance structure.

Change History (7)

comment:1 by Daniel Hillier, 7 years ago

Description: modified (diff)
Owner: changed from daniel.hillier@… to Daniel Hillier

The proposed fix tests for self-referential relationships and avoids the multiple concrete model inheritance path in those instances.

PR

Last edited 7 years ago by Tim Graham (previous) (diff)

comment:2 by Tim Graham, 7 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Tim Graham, 7 years ago

Patch needs improvement: set

I left comments for improvement on the PR. Please uncheck "Patch needs improvement" after updating. A single squashed commit is fine. Thanks!

comment:4 by Daniel Hillier, 7 years ago

Patch needs improvement: unset

Thanks for the review!

I've simplified the way I fixed the bug and implemented your suggestions.

I think the heart of the bug was the definition of from_parent. A relationship between two models isn't from_parent if the two model classes are the same. I've updated how from_parent is defined to reflect that corner case.

Happy to incorporate any further changes you may have.

comment:5 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In 7da37699:

Fixed #27594 -- Fixed select_related() with reverse self-referential OneToOneField.

Fixed definition of klass_info['from_parent'] so that two models aren't
considered from a parent class if the model classes are the same.

comment:6 by Tim Graham <timograham@…>, 7 years ago

In 4a9f9cc5:

Fixed #27594 -- Fixed select_related() with reverse self-referential OneToOneField.

Fix definition of klass_info['from_parent']. The relationship between
two models shouldn't be considered as being from a parent class if the
model classes are the same.

Thanks Tim for the review.

comment:7 by Tim Graham <timograham@…>, 7 years ago

In 01b3dc22:

[1.8.x] Reverted "Fixed #27594 -- Fixed select_related() with reverse self-referential OneToOneField."

This reverts commit 4a9f9cc521f09c60f56024eb6688e30ab86b998f. It was
accidentally pushed to this branch.

Note: See TracTickets for help on using tickets.
Back to Top