Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#3779 closed (fixed)

Problem with ManyToManyFields and syncdb

Reported by: Ben Slavin <benjamin.slavin@…> Owned by: adrian
Component: Core (Other) Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


The creation of ManyToMany tables in the database through syncdb is prone to failure, and depends on the ordering of INSTALLED_APPS in syncdb uses a deferral system (seen_models and pending_references) to ensure proper setup of model tables in the database. This allows the tables to be created without references, and constraints to be added after the related tables exist.

Currently, the creation of ManyToMany tables (in does not use a deferral mechanism, and will fail to create the tables properly. If the models are loaded in the right order, this isn't an issue; however, if the they are loaded in reverse order and the database is strict about enforcing references (PostgreSQL), this is problematic.

Note: This doesn't impact MySQL (MyISAM) or sqlite because of the way they handle references -- they don't.

Here's an example:


from django.db import models
from import Foo
class Bar(models.Model):
    foos = models.ManyToManyField(Foo)


from django.db import models
class Foo(models.Model):
    a = models.IntegerField() excerpt (working version)

INSTALLED_APPS = ('','') excerpt (non-working version)

INSTALLED_APPS = ('','') syncdb output for non-working version

Creating table bar_bar
Traceback (most recent call last):
  File "./", line 11, in ?
  File "/usr/lib/python2.4/site-packages/django/core/", line 1666, in execute_manager
    execute_from_command_line(action_mapping, argv)
  File "/usr/lib/python2.4/site-packages/django/core/", line 1565, in execute_from_command_line
    action_mapping[action](int(options.verbosity), options.interactive)
  File "/usr/lib/python2.4/site-packages/django/core/", line 543, in syncdb
  File "/usr/lib/python2.4/site-packages/django/db/backends/", line 12, in execute
    return self.cursor.execute(sql, params)
  File "/usr/lib/python2.4/site-packages/django/db/backends/postgresql/", line 43, in execute
    return self.cursor.execute(sql, [smart_basestring(p, self.charset) for p in params])
psycopg.ProgrammingError: ERROR:  relation "foo_foo" does not exist

CREATE TABLE "bar_bar_foos" (
    "id" serial NOT NULL PRIMARY KEY,
    "bar_id" integer NOT NULL REFERENCES "bar_bar" ("id") DEFERRABLE INITIALLY DEFERRED,
    "foo_id" integer NOT NULL REFERENCES "foo_foo" ("id") DEFERRABLE INITIALLY DEFERRED,
    UNIQUE ("bar_id", "foo_id")

The problem is that "foo_id" should be "integer NOT NULL" and an ALTER TABLE statement should be issued for bar_bar_foos after the foo_foo table is created, rather than trying to create the reference initially.

Change History (3)

comment:1 Changed 9 years ago by Gary Wilson <gary.wilson@…>

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

comment:2 Changed 9 years ago by russellm

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

(In [4780]) Fixed #3779 -- Resolved problem with order of creation of m2m tables during syncdb. Well spotted, Ben Slavin, and thanks for the very helpful test case.

comment:3 Changed 9 years ago by Ben Slavin <benjamin.slavin@…>

Fix confirmed.

Thanks Russ

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