Opened 10 years ago

Closed 5 years ago

#4212 closed Bug (invalid)

badly-defined ForeignKey to self does not cause an exception

Reported by: Forest Bond <forest@…> Owned by: Jonas Obrist
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a model something like this:

class MyModel(models.Model):
    my_model = ForeignKey('MyModel')

This mostly works, but I was confused when I found that instances didn't have a my_model_set attribute. I found that I should've defined my model like this:

class MyModel(models.Model):
    my_model = ForeignKey('self')

I can do that just fine, of course, but it was a bit non-obvious what the problem was. It'd be nice if defining that relationship improperly threw an exception.

Change History (6)

comment:1 Changed 10 years ago by James Bennett

Triage Stage: UnreviewedAccepted

Unfortunately it's kind of tricky to catch because that form is legal for getting around forward references to as-yet-undefined model classes, and for dealing with circular model dependencies. Probably still a good idea to see if we can do anything about it, though.

comment:2 Changed 9 years ago by Philippe Raoult

i'd say we should force related_name to be set for foreignkeys to self.

comment:3 Changed 9 years ago by Ramiro Morales

#3914 was about the same issue, marked as duplicate

comment:4 Changed 6 years ago by Łukasz Rekucki

Severity: Normal
Type: Bug

comment:5 Changed 5 years ago by Jonas Obrist

Easy pickings: unset
Owner: changed from nobody to Jonas Obrist
UI/UX: unset

comment:6 Changed 5 years ago by Jonas Obrist

Resolution: invalid
Status: newclosed

Can't reproduce this issue, works fine for me.

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