Modify ↓
Ticket #512 (closed defect: fixed)
Incorrect primary key detection in complex model relationships
| Reported by: | DrMeers@… | Owned by: | andrew |
|---|---|---|---|
| Priority: | major | Milestone: | 0.7.3 |
| Component: | migrations | Version: | 0.7.1 |
| Keywords: | Cc: |
Description
Hard to explain in a sentence; here is a simplified example:
demo/models.py:
rom django.db import models
class RelatableContent(models.Model):
related_items = models.ManyToManyField('RelatableContent', symmetrical=True)
class WikiPage(RelatableContent):
parent = models.ForeignKey('self')
class Meta:
abstract = True
class MyWiki(WikiPage):
pass
Create migration:
$ ./manage.py schemamigration --initial demo Creating migrations directory at '.../demo/migrations'... Creating __init__.py in '.../demo/migrations'... + Added model demo.RelatableContent + Added M2M table for related_items on demo.RelatableContent + Added model demo.MyWiki Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate demo
Run migration:
./manage.py migrate demo Running migrations for demo: - Migrating forwards to 0001_initial. > demo:0001_initial Traceback (most recent call last): ... django.db.utils.DatabaseError: column "id" referenced in foreign key constraint does not exist
Problem SQL statement:
ALTER TABLE "demo_mywiki" ADD CONSTRAINT "parent_id_refs_id_2d9983b3" FOREIGN KEY ("parent_id") REFERENCES "demo_mywiki" ("id") DEFERRABLE INITIALLY DEFERRED;
Desired output:
$ ./manage.py sqlall demo
BEGIN;
CREATE TABLE "demo_relatablecontent_related_items" (
"id" serial NOT NULL PRIMARY KEY,
"from_relatablecontent_id" integer NOT NULL,
"to_relatablecontent_id" integer NOT NULL,
UNIQUE ("from_relatablecontent_id", "to_relatablecontent_id")
)
;
CREATE TABLE "demo_relatablecontent" (
"id" serial NOT NULL PRIMARY KEY
)
;
ALTER TABLE "demo_relatablecontent_related_items" ADD CONSTRAINT "from_relatablecontent_id_refs_id_19b1eb18" FOREIGN KEY ("from_relatablecontent_id") REFERENCES "demo_relatablecontent" ("id") DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE "demo_relatablecontent_related_items" ADD CONSTRAINT "to_relatablecontent_id_refs_id_19b1eb18" FOREIGN KEY ("to_relatablecontent_id") REFERENCES "demo_relatablecontent" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE TABLE "demo_mywiki" (
"relatablecontent_ptr_id" integer NOT NULL UNIQUE REFERENCES "demo_relatablecontent" ("id") DEFERRABLE INITIALLY DEFERRED,
"parent_id" integer NOT NULL
)
;
ALTER TABLE "demo_mywiki" ADD CONSTRAINT "parent_id_refs_relatablecontent_ptr_id_2d9983b3" FOREIGN KEY ("parent_id") REFERENCES "demo_mywiki" ("relatablecontent_ptr_id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX "demo_mywiki_parent_id" ON "demo_mywiki" ("parent_id");
COMMIT;
Using PostgreSQL.
Attachments
Change History
Changed 3 years ago by Aram Dulyan <aram00@…>
- Attachment 512_fix.diff added
Workaround Django bug #13987
comment:3 Changed 3 years ago by Aram Dulyan <aram00@…>
This issue stems from Django bug #13987 (see link for more details). I've attached a diff that provides a future-safe workaround for this problem (tests pass).
comment:4 Changed 3 years ago by andrew
- Status changed from assigned to closed
- Resolution set to fixed
Thanks, looks good. Committed as [5fe9e105acf3].
Note: See
TracTickets for help on using
tickets.

Hm, that seems possible. I'll put it on the list of things to fix...