Opened 7 years ago

Closed 7 years ago

#6475 closed (duplicate)

some SQL constraint statements commented out in generated sql

Reported by: railk Owned by: nobody
Component: Core (Other) Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

To Reproduce

Set up a django project and app, and set up an appropriate database. Put the following code in the app's models.py:

from django.db import models

class A(models.Model):
    b_ref = models.ForeignKey('B')

class B(models.Model):
    a_ref = models.ForeignKey(A)

Then run python manage.py sql <appname>

Output

BEGIN;
CREATE TABLE `testapp_a` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `b_ref_id` integer NOT NULL
)
;
CREATE TABLE `testapp_b` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `a_ref_id` integer NOT NULL
)
;
ALTER TABLE `testapp_a` ADD CONSTRAINT b_ref_id_refs_id_2727239a FOREIGN KEY (`b_ref_id`) REFERENCES `testapp_b` (`id`);
-- The following references should be added but depend on non-existent tables:
-- ALTER TABLE `testapp_b` ADD CONSTRAINT a_ref_id_refs_id_3bce8de8 FOREIGN KEY (`a_ref_id`) REFERENCES `testapp_a` (`id`);
COMMIT;

Expected Output

Same as output except that the last ALTER TABLE statement should not be commented out.

Patch

I have made a patch, but my python is a bit rusty...

Attachments (1)

django-sql-constraints-fix.diff (3.3 KB) - added by railk 7 years ago.
fix for commented-out constraints. my python is rusty, so may need cleanup.

Download all attachments as: .zip

Change History (4)

Changed 7 years ago by railk

fix for commented-out constraints. my python is rusty, so may need cleanup.

comment:1 Changed 7 years ago by ramiro

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Which database engine/backend are you using?. See #4193

comment:2 Changed 7 years ago by railk

It's not the backend, it's the circular referencing; Previously, when a model was processed in django.core.management.sql.sql_create, only pending references with this current model as the reference target where processed. The patch modifies sql_for_pending_references so it processed all references where the current model is the target *and* all references where the target is a known model and the reference's source is the current model... did that make any sense?

But to answer your question, I'm using MySQL.

comment:3 Changed 7 years ago by ramiro

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

Duplicate of #4193

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