Project

General

Profile

Dbo and Composite Foreign Keys

Added by Michael Wagner over 13 years ago

Hi Koen,

I am still facing a problem with a more "complex" database schema.

I have two tables "map" and "presentation" both have a composite primary key (columns "id" and "idMandator"). One "map" can have many "presentation"s and the foreign key in table "presentation" is a composite one consisting of a field "map_id" and again field "idMandator". Here is how it looks like:

create table map (

id integer not null,

"idMandator" smallint not null,

name text not null,

description text,

version int

);

alter table map add constraint pk_map primary key (id, "idMandator");

create table presentation (

id integer not null,

"idMandator" smallint not null,

aliasname text,

map_id integer,

version int

);

alter table presentation add constraint pk_presentation primary key (id, "idMandator");

alter table presentation add constraint fk_presentation_map foreign key (map_id, "idMandator") references map (id, "idMandator");

I added the "map" to a "presentation" like this:

Wt::Dbo::ptr mapPtr = session_.add(map);

Wt::Dbo::ptr presentationPtr = session_.add(presentation);

presentationPtr.modify()->setMap(mapPtr);

and wt creates an SQL statement as follows:

insert into "presentation" ("version", "idMandator", "id", "aliasname", "idMandator", "map_id") values ($1, $2, $3, $4, $5, $6)

This of course causes a Postgres error as: ...column "idMandator" specified more than once....

The issue is that the field "idMandator" in table "presentation" is part of the primary key as well as of the foreign key. Currently wt does not check for this case and simply adds and binds "idMandator" to the SQL statement again. So perhaps a fix could be just to check if a field with the same name is already bound to the SQL statement and in this case it won't be added again.

It would be great if you could fix this issue since it's the last obstacle left to use wt with composite primary / foreign keys.

Many thanks again,

Michael


Replies (2)

RE: Dbo and Composite Foreign Keys - Added by Koen Deforche over 13 years ago

Hey Michael,

Looking at this issue, I see one open question: in your database you reuse the idMandator both as part of the foreign key and the primary key. But in your C mapping they are represented by two different values (two instances of your CompositeKey). Even if we solve the issue for using the value read from the database for idMandator twice, there is no gaurantee that in the C domain objec these values do not go out of sync. So when trying to save the object to the database, a choice needs to be made: which value will be used to save in the idMandator field ?

Is there something I am missing, like some implicit linkage between the two idmandator values ?

Regards,

koen

RE: Dbo and Composite Foreign Keys - Added by Michael Wagner over 13 years ago

Hi Koen,

Sorry for my late reply, I was traveling the last days. Theoretically the "idMandator" should be the same throughout all table relationships but it's up to me to take care that they don't go out of sync. I now added an additional column "mapMandator" to the "presentation" table that references "idMandator" of the "map" table. That works so there is not need for a fix from my side any longer.

Thanks a lot,

Michael

    (1-2/2)