Changes between Version 3 and Version 4 of ModelInheritance
- Timestamp:
- Jan 26, 2006, 6:01:32 PM (19 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ModelInheritance
v3 v4 36 36 === 1. Modeling parent relations in SQL? === 37 37 38 1. Foreign key to parent table named $(parenttable)_id 39 2. Foreign key to parent table named 'parent_id' 40 3. 'id' of parent row equals 'id' of child row 38 The general consesus seems to be this: 41 39 42 1. and 2. would take up part of the objects namespace. With 1, you couldn't have attributes named $(parenttable)_id or 'parent_id' respectively.43 44 With option 3, auto-incrementing might be harder, but it makes more sense to have 1 id across the board. I think one would expect {{{Place.objects.get(2)}}} to return the "same" object as {{{Restaurant.objects.get(2)}}}.45 46 Here's an example of option 1:47 40 {{{ 48 41 #!sql … … 53 46 54 47 CREATE TABLE "myapp_restaurant" ( 55 "id" integer NOT NULL PRIMARY KEY,56 " place_id" integer NOT NULL REFERENCES "myapp_place" ("id"),48 /* PRIMARY KEY REFERENCES "myapp_places" ("id") works for postgres, what about others? */ 49 "id" integer NOT NULL PRIMARY KEY REFERENCES "myapp_places" ("id"), 57 50 "description" text NOT NULL 58 51 ); 59 52 60 53 CREATE TABLE "myapp_italianrestaurant" ( 61 "id" integer NOT NULL PRIMARY KEY, 62 "restaurant_id" integer NOT NULL REFERENCES "myapp_restaurant" ("id"), 63 "has_decent_gnocci" bool NOT NULL 64 ); 65 }}} 66 67 and option 3: 68 {{{ 69 #!sql 70 CREATE TABLE "myapp_place" ( 71 "id" integer NOT NULL PRIMARY KEY, 72 "name" varchar(50) NOT NULL 73 ); 74 75 CREATE TABLE "myapp_restaurant" ( 76 "id" integer NOT NULL PRIMARY KEY, /* Could this reference myapp_place's id too? Doubtful. :( */ 77 "description" text NOT NULL 78 ); 79 80 CREATE TABLE "myapp_italianrestaurant" ( 81 "id" integer NOT NULL PRIMARY KEY, /* Ditto for referencing myapp_restaurant's id */ 54 "id" integer NOT NULL PRIMARY KEY REFERENCES "myapp_restaurant" ("id"), 82 55 "has_decent_gnocci" bool NOT NULL 83 56 );