On Nov 19, 2003, at 4:03 AM, Tore Halset wrote:
>
> My database has two tables that look like this. One car can have one
> engine and one engine can only be in one car.
>
> create table car (car_id int primary key, engine_id int);
> create table engine (engine_id int);
>
> That looks like a one-to-many, but the car.engine_id column has a
> unique
> index.
>
> I have defined this as a one-to-one relationship in cayenne modeler,
> but
> the relation from engine to car does not work. engine.getCar() does
> return
> one object, but all of the attributes are null.
I have to look for the exact reasons why it breaks. The workaround
you've mentioned makes total sense and should work just fine. I opened
a bug report (classified as "improvement request"):
http://objectstyle.org/jira/secure/ViewIssue.jspa?key=CAY-41
> When I try to create a new database from cayenne modeler the following
> erromessage are given.
>
> "Schema Generation Error - Failed statement: ALTER TABLE engine ADD
> FOREIGN KEY (engine_id) REFERENCES car (engine_id) - ERROR: UNIQUE
> constraint matching given keys for referenced table car not found"
This is cause CayenneModeler doesn't support generating UNIQUE
constraints and naively tries to create an FK constraint referencing
another FK, so the database complains.
On a side note, have you ever thought of a modeling this in DB as an
optional PK->PK "master->dependent" relationship? Such one-one
relationships are fully supported in Cayenne. Since from your schema
"engine" can't exist without a car record anyway.
Andrus
This archive was generated by hypermail 2.0.0 : Thu Nov 20 2003 - 13:32:35 EST