Opened 16 years ago

Closed 16 years ago

Last modified 13 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: dev
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: no UI/UX: no

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@… 16 years ago.
a models.py file that fails during validation
note1.py (402 bytes ) - added by rock@… 16 years ago.
7908.patch (610 bytes ) - added by Matthias Kestenholz 16 years ago.

Download all attachments as: .zip

Change History (8)

by rock@…, 16 years ago

Attachment: models.py added

a models.py file that fails during validation

comment:1 by rock@…, 16 years ago

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.

by rock@…, 16 years ago

Attachment: note1.py added

by Matthias Kestenholz, 16 years ago

Attachment: 7908.patch added

comment:2 by Matthias Kestenholz, 16 years ago

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 by Malcolm Tredinnick, 16 years ago

milestone: 1.0

comment:4 by Russell Keith-Magee, 16 years ago

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 by Jacob, 13 years ago

milestone: 1.0

Milestone 1.0 deleted

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