Opened 8 years ago

Closed 8 years ago

Last modified 5 years ago

#7908 closed (fixed)

A ForeignKey of an abstract class can fail to validate

Reported by: rockmhoward@… Owned by: nobody
Component: Core (Other) Version: master
Severity: Keywords: ForeignKey abstract
Cc: mk@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

A model with a ForeignKey relating to an abstract class fails to validate by crashing in related.py line 605.

The example models.py is attached or see: http://dpaste.com/66858/

The traceback from manage.py validate: http://dpaste.com/66860/

Attachments (3)

models.py (815 bytes) - added by rock@… 8 years ago.
a models.py file that fails during validation
note1.py (402 bytes) - added by rock@… 8 years ago.
7908.patch (610 bytes) - added by Matthias Kestenholz 8 years ago.

Download all attachments as: .zip

Change History (8)

Changed 8 years ago by rock@…

Attachment: models.py added

a models.py file that fails during validation

comment:1 Changed 8 years ago by rock@…

I tried adding a guard by replacing the offending line 605 as follows:

to_field = to_field
if to_field == None:

if to._meta.pk is None:

raise ValueError("The %s model does not have a primary key and therefore a ForeignKey cannot relate to it." % to_name)

to_field = to._meta.pk.name

This is not a real fix as the validate command still crashes, but the error message is a bit nicer. A real fix has to go in the validate code.

Changed 8 years ago by rock@…

Attachment: note1.py added

Changed 8 years ago by Matthias Kestenholz

Attachment: 7908.patch added

comment:2 Changed 8 years ago by Matthias Kestenholz

Cc: mk@… added
Has patch: set
Triage Stage: UnreviewedReady for checkin

The attached patch gives a better(?) error message for both ForeignKey and ManyToManyField, using the syntax above and also using f.e. mine = models.ManyToManyField('Attachment') (lazy loading)

comment:3 Changed 8 years ago by Malcolm Tredinnick

milestone: 1.0

comment:4 Changed 8 years ago by Russell Keith-Magee

Resolution: fixed
Status: newclosed

(In [8442]) Fixed #7908: Added validation checks on attempts to create ForeignKey and M2M relations with abstract classes. Thanks to Rock Howard for the report.

comment:5 Changed 5 years ago by Jacob

milestone: 1.0

Milestone 1.0 deleted

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