Opened 11 years ago

Closed 10 years ago

Last modified 9 years ago

#2260 closed defect (fixed)

[patch] Model tables get created in the wrong order

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


Django's sqlreset sometimes generates CREATE statements in the wrong order.

My file:

from django.db import models

class AuctionElement(models.Model):
    name = models.CharField(maxlength=500)

class AuctionDetails(models.Model):
    element = models.ForeignKey(AuctionElement)
    name = models.CharField(maxlength=500)

At least on my machine Django tries to create a table for AuctionDetails before AuctionElement. It might be difficult to reproduce since the result depends on model names: change them a bit and you will get different order.

As far as I can tell Django neither keeps track of model sequence nor tries to order them by dependencies. The order as defined in is lost in db.models.loading.register_models.

Attachments (1)

fix_tables_created_in_wrong_order.diff (1.1 KB) - added by marcink@… 10 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 11 years ago by mir@…

Cc: mir@… added

Which version did you use? There has been a lot of changes in the last few days, and I think this is all fixed by issueing the foreign key constraints after both involved tables have been created.

comment:2 Changed 11 years ago by marcink@…

I checked right now in rev 3236. The SQL created specifies foreign key references inside CREATE TABLE statements.

comment:3 Changed 10 years ago by Matt McClanahan

Resolution: fixed
Status: newclosed

This appears to be fixed.

Using rev 3519, postgres, and the models in the ticket description, sql scratch yields:

CREATE TABLE "scratch_auctiondetails" (
    "id" serial NOT NULL PRIMARY KEY,
    "element_id" integer NOT NULL,
    "name" varchar(500) NOT NULL
CREATE TABLE "scratch_auctionelement" (
    "id" serial NOT NULL PRIMARY KEY,
    "name" varchar(500) NOT NULL
ALTER TABLE "scratch_auctiondetails" ADD CONSTRAINT element_id_refs_id_75764088 FOREIGN KEY ("element_id") REFERENCES "scratch_auctionelement" ("id");

If it's still an issue, which database are you using?

comment:4 Changed 10 years ago by marcink@…

Resolution: fixed
Status: closedreopened
Version: SVN

The problem apprears when you call sql or sqlreset on a database that
already contains those tables. This is usually the case with

Django declares foreign keys outside of CREATE TABLE only if the other
table does not exist in the database yet. If you call sql or sqlreset
with an already populated database then it is very likely that the
table does exist at the moment Django checks for it.

Changed 10 years ago by marcink@…

comment:5 Changed 10 years ago by marcink@…

Summary: Model tables get created in the wrong order[patch] Model tables get created in the wrong order

The attached patch changes get_sql_create to assume that all models
belonging to the application being installed are not present or will
be dropped form the database before executing CREATE statements.

comment:6 Changed 10 years ago by Malcolm Tredinnick

Resolution: fixed
Status: reopenedclosed

(In [3542]) Fixed #2260 -- fixed a problem where some foreign key references were being
omitted during a "reset" or "sqlreset". Thanks, marcink@….

comment:7 Changed 9 years ago by anonymous

seduction Here's my post on how to get laid in the next month or two. This post does not maximize your chances of "getting really good at pickup" - in fact, it does the opposite.

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