Opened 15 years ago

Closed 15 years ago

Last modified 14 years ago

#258 closed defect (invalid)

Cannot create many-many relationships within the same table

Reported by: andrew@… Owned by: Adrian Holovaty
Component: Database layer (models, ORM) Version:
Severity: major Keywords: sql ForeignKey
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


This model fails:

from django.core import meta

# Create your models here.
class Object(meta.Model):
    fields = (
        meta.CharField('title', maxlength=50),

class Relate(meta.Model):
    fields = (
        meta.ForeignKey(Object, rel_name='parent'),
        meta.ForeignKey(Object, rel_name='child'),

When you generate SQL for it, two columns with the name 'object_id' are placed in the 'relate' table. I was expecting columns with names 'parent_id' and 'child_id'.

Change History (2)

comment:1 Changed 15 years ago by Jacob

Resolution: invalid
Status: newclosed

Your foreign keys need name attributes; it uses the name of the related-to object by default, so there's a clash (rel_name is used for the names of the helper methods -- get_parent, get_child, etc.)

comment:2 Changed 15 years ago by andrew@…

OK, this works, thanks :).

Any chance you could update the documentation to include it? At the moment, only ManyToManyField() shows the name option.

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