Hi all,
I wanted to fix this bug. I just went through the code to see what was happening. For the same sample code given in the ticket, these were my findings:
The schema generated looks like this and there is no issue with it:
CREATE TABLE "testapp_childmodel" (
"parentmodel_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "testapp_parentmodel" ("id"),
"text" text NOT NULL
);
CREATE TABLE "testapp_parentmodel" (
"id" integer NOT NULL PRIMARY KEY,
"slug" varchar(50) NOT NULL
);
CREATE TABLE "testapp_parentmodel_links" (
"id" integer NOT NULL PRIMARY KEY,
"from_parentmodel_id" integer NOT NULL REFERENCES "testapp_parentmodel" ("id"),
"to_parentmodel_id" integer NOT NULL REFERENCES "testapp_parentmodel" ("id"),
UNIQUE ("from_parentmodel_id", "to_parentmodel_id")
);
CREATE INDEX "testapp_parentmodel_slug" ON "testapp_parentmodel" ("slug");
I inserted test1, test2 and test3(all names being values of slug field) to ParentModel and linked test1 and test3 to test2 through links field. There seems to be no issues. Then I inserted testc1, testc2 and testc3 to ChildModel and linked testc2 and testc3 to testc1 through inherited links field. Now I can see only to links, i.e
I get these results in the interactive interpreter:
>>> from dummy.testapp.models import ParentModel, ChildModel
>>> c1 = ChildModel.objects.get(slug='testc1')
>>> c2 = ChildModel.objects.get(slug='testc2')
>>> c3 = ChildModel.objects.get(slug='testc3')
>>> c1.links.all()
[]
>>> c2.links.all()
[<ParentModel: testc1>]
>>> c3.links.all()
[<ParentModel: testc1>]
This means only to links are being queried and not from links for the inherited model. I just saw how the queries are working and these are the queries constructed for c1, c2 and c3 respectively.
SELECT "testapp_parentmodel"."id", "testapp_parentmodel"."slug" FROM "testapp_parentmodel" INNER JOIN "testapp_parentmodel_links" ON ("testapp_parentmodel"."id" = "testapp_parentmodel_links"."to_parentmodel_id") WHERE "testapp_parentmodel_links"."from_parentmodel_id" = 4 LIMIT 21
SELECT "testapp_parentmodel"."id", "testapp_parentmodel"."slug" FROM "testapp_parentmodel" INNER JOIN "testapp_parentmodel_links" ON ("testapp_parentmodel"."id" = "testapp_parentmodel_links"."to_parentmodel_id") WHERE "testapp_parentmodel_links"."from_parentmodel_id" = 5 LIMIT 21
SELECT "testapp_parentmodel"."id", "testapp_parentmodel"."slug" FROM "testapp_parentmodel" INNER JOIN "testapp_parentmodel_links" ON ("testapp_parentmodel"."id" = "testapp_parentmodel_links"."to_parentmodel_id") WHERE "testapp_parentmodel_links"."from_parentmodel_id" = 6 LIMIT 21
From what I see the queries must be actually UNION of two queries which fetch data both from, "from" and "to" fields in the "testapp_parentmodel_links" table.It should look some thing like this, from what I have understood:
SELECT "testapp_parentmodel"."id", "testapp_parentmodel"."slug" FROM "testapp_parentmodel" INNER JOIN "testapp_parentmodel_links" ON ("testapp_parentmodel"."id" = "testapp_parentmodel_links"."to_parentmodel_id") WHERE "testapp_parentmodel_links"."from_parentmodel_id" = 4 LIMIT 21
UNION
SELECT "testapp_parentmodel"."id", "testapp_parentmodel"."slug" FROM "testapp_parentmodel" INNER JOIN "testapp_parentmodel_links" ON ("testapp_parentmodel"."id" = "testapp_parentmodel_links"."from_parentmodel_id") WHERE "testapp_parentmodel_links"."to_parentmodel_id" = 4 LIMIT 21
Is this an elegant approach? If so can some one help me implement this? I went through the code of QuerySet, Manager classes and other parts of django.db.models. How to go about from here? I am very keen on fixing this issue. Kindly help me.
--
Thanks and regards,
Madhusudan.C.S
Blogs at:
www.madhusudancs.infoOfficial Email ID:
madhu...@madhusudancs.info
P.S: I am really sorry if the text is not wrapped. I am still not understanding how wrapping works here. Will correct it from next mail. Hope Auto text wrapping works.