Opened 10 years ago

Closed 10 years ago

Last modified 9 years ago

#258 closed defect (invalid)

Cannot create many-many relationships within the same table

Reported by: andrew@… Owned by: adrian
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: UI/UX:

Description

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 10 years ago by jacob

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

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 10 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