views:

26

answers:

0

It seems to me that the current django release (starting with 1.2) requires a many-2-many join table to have its own id field and that this key is its primary key. Is that true?

I have some applications which are using legacy databases in which the many-2-many table have primary keys consisting of both foreign keys. But those applications are now generating errors because of missing id columns. I am pretty sure that when using Django 1.x these applications were working.

Also when I look at the sql file generated with manage.py sql application, the m-2-m table contain an id column.

For instance I have these tables created using a ERD program:

CREATE TABLE product (
    id SERIAL NOT NULL,
    name CHARACTER VARYING(64),
    image_file CHARACTER VARYING(256),
    price NUMERIC(7,2),
    CONSTRAINT PK_product PRIMARY KEY (id)
);

CREATE TABLE material (
    id SERIAL NOT NULL,
    material_code CHARACTER VARYING(10) NOT NULL,
    material_name CHARACTER VARYING(32) NOT NULL,
    CONSTRAINT PK_material PRIMARY KEY (id)
);

CREATE TABLE product_material (
    product_id INTEGER NOT NULL,
    material_id INTEGER NOT NULL,
    CONSTRAINT PK_product_material PRIMARY KEY ( product_id, material_id )
);

But it seems Django doesn't like the fact that the table product_material doesn't have its own primary key called 'id'. Django manage.py sql application creates a table:

CREATE TABLE product_material (
    id SERIAL NOT NULL PRIMARY KEY
    product_id INTEGER NOT NULL,
    material_id INTEGER NOT NULL NOT NULL REFERENCES "material" ("id") DEFERRABLE INITIALLY DEFERRED,
    UNIQUE ("product_id", "material_id")
);

ALTER TABLE "product_material" ADD CONSTRAINT "product_id_refs_id_95be5f02" FOREIGN KEY ("product_id") REFERENCES "product" ("id") DEFERRABLE INITIALLY DEFERRED;

So how does Django now deal with legacy databases?