Opened 9 years ago

Closed 6 years ago

#3077 closed defect (fixed)

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

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

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@…> 9 years ago.
patch against current trunk
3077-test.diff (1.5 KB) - added by adurdin 6 years ago.
Regression test

Download all attachments as: .zip

Change History (8)

Changed 9 years ago by Nikolaus Schlemm <nikl@…>

patch against current trunk

comment:1 Changed 9 years ago by Nikolaus Schlemm <nikl@…>

  • Summary changed from multi-degree ForeignKey/OneToOne relationships with non-integer primary key to [patch] multi-degree ForeignKey/OneToOne relationships with non-integer primary key

works for me

comment:2 Changed 9 years ago by adrian

  • Component changed from Core framework to Database wrapper

comment:3 Changed 8 years ago by anonymous

  • milestone Version 1.0 deleted

Milestone Version 1.0 deleted

comment:4 Changed 8 years ago by Gary Wilson <gary.wilson@…>

  • Needs tests set
  • Triage Stage changed from Unreviewed to Accepted

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

comment:5 Changed 6 years ago by adurdin

  • Owner changed from nobody to adurdin
  • Status changed from new to assigned

Changed 6 years ago by adurdin

Regression test

comment:6 Changed 6 years ago by adurdin

  • Has patch unset
  • Keywords qsrf added; multi degree foreign key one to one relationship non integer primary key removed
  • Needs tests unset
  • Resolution set to fixed
  • Status changed from assigned to closed

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