Opened 18 years ago

Closed 16 years ago

#3077 closed defect (fixed)

[patch] multi-degree ForeignKey/OneToOne relationships with non-integer primary key

Reported by: Nikolaus Schlemm <nikl@…> Owned by: Andy Durdin
Component: Database layer (models, ORM) Version: dev
Severity: major Keywords: qsrf
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

this is closely related to ForeignOne-to-one relations - different primary_key and Small patch to make One to One fields work with any data type:

imagine a setup like this:

class A(models.Model):
    key = models.CharField(maxlength=10, primary_key=True)

class B(models.Model):
    a = models.OneToOneField(A)

class C(models.Model):
    a = models.ForeignKey(A)
    b = models.OneToOneField(B)

class D(models.Model):
    a = models.ForeignKey(A)
    b = models.ForeignKey(B)
    c = models.OneToOneField(C)

while django correctly constructs

CREATE TABLE "testapp_a" (
    "key" varchar(10) NOT NULL PRIMARY KEY
);
CREATE TABLE "testapp_b" (
    "a_id" varchar(10) NOT NULL PRIMARY KEY REFERENCES "testapp_a" ("key")
);

but it erronously constructs the fields farther away with integer fields:

CREATE TABLE "testapp_c" (
    "a_id" varchar(10) NOT NULL REFERENCES "testapp_a" ("key")
    "b_id" integer NOT NULL PRIMARY KEY,
);
CREATE TABLE "testapp_d" (
    "a_id" varchar(10) NOT NULL REFERENCES "testapp_a" ("key")
    "b_id" integer NOT NULL REFERENCES "testapp_b" ("a_id"),
    "c_id" integer NOT NULL PRIMARY KEY REFERENCES "testapp_c" ("b_id"),
);

I'll try to come up with a patch asap

Attachments (2)

svn.diff (682 bytes ) - added by Nikolaus Schlemm <nikl@…> 18 years ago.
patch against current trunk
3077-test.diff (1.5 KB ) - added by Andy Durdin 16 years ago.
Regression test

Download all attachments as: .zip

Change History (8)

by Nikolaus Schlemm <nikl@…>, 18 years ago

Attachment: svn.diff added

patch against current trunk

comment:1 by Nikolaus Schlemm <nikl@…>, 18 years ago

Summary: multi-degree ForeignKey/OneToOne relationships with non-integer primary key[patch] multi-degree ForeignKey/OneToOne relationships with non-integer primary key

works for me

comment:2 by Adrian Holovaty, 18 years ago

Component: Core frameworkDatabase wrapper

comment:3 by (none), 18 years ago

milestone: Version 1.0

Milestone Version 1.0 deleted

comment:4 by Gary Wilson <gary.wilson@…>, 18 years ago

Needs tests: set
Triage Stage: UnreviewedAccepted

It would be great if someone could take the code example here and turn it into a unit test.

comment:5 by Andy Durdin, 16 years ago

Owner: changed from nobody to Andy Durdin
Status: newassigned

by Andy Durdin, 16 years ago

Attachment: 3077-test.diff added

Regression test

comment:6 by Andy Durdin, 16 years ago

Has patch: unset
Keywords: qsrf added; multi degree foreign key one to one relationship non integer primary key removed
Needs tests: unset
Resolution: fixed
Status: assignedclosed

I've attached a regression test for this bug, but I cannot reproduce this as of r10680. I presume it got fixed as part of queryset-refactor.

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