Opened 9 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: ojii
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


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 9 years ago by ubernostrum

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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 PhiR

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

comment:3 Changed 9 years ago by ramiro

#3914 was about the same issue, marked as duplicate

comment:4 Changed 5 years ago by lrekucki

  • Severity set to Normal
  • Type set to Bug

comment:5 Changed 5 years ago by ojii

  • Easy pickings unset
  • Owner changed from nobody to ojii
  • UI/UX unset

comment:6 Changed 5 years ago by ojii

  • Resolution set to invalid
  • Status changed from new to closed

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

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