Changes between Version 3 and Version 4 of ModelInheritance


Ignore:
Timestamp:
Jan 26, 2006, 6:01:32 PM (19 years ago)
Author:
jkocherhans
Comment:

updated to use option 3 for modeling parent relations in sql

Legend:

Unmodified
Added
Removed
Modified
  • ModelInheritance

    v3 v4  
    3636=== 1. Modeling parent relations in SQL? ===
    3737
    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
     38The general consesus seems to be this:
    4139
    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:
    4740{{{
    4841#!sql
     
    5346
    5447CREATE 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"),
    5750    "description" text NOT NULL
    5851);
    5952
    6053CREATE 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"),
    8255    "has_decent_gnocci" bool NOT NULL
    8356);
Back to Top