Multicolumn primary keys and multicolumn foreign keys

From: Ferruccio Zamuner <nonsolosoft(at)diff(dot)org>
To: pgsql-sql(at)postgresql(dot)org
Subject: Multicolumn primary keys and multicolumn foreign keys
Date: 2001-01-28 10:34:35
Message-ID: 3A73F5BB.F106BB29@diff.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi,

I've found a trouble and I've tried to avoid it without success:

-------
create table companies (
id serial not null primary key,
firm_name text not null,
activity text
);

create table customers (
id int not null references companies,
seller_id int not null references companies,
note text,
primary key (id,seller_id)
);

create table invoices (
seller_id int4 not null references companies, /* who send invoice
*/
customer_id int4 not null, /* who receive the invoice and pay
for it */
invoice_no int4 not null unique,
invoice_date date,

primary key (seller_id,invoice_no,invoice_date),
foreign key (seller_id, customer_id) references customers
);

INSERT INTO "companies" ("firm_name","activity") VALUES
('NonSoLoSoft','ASP');
INSERT INTO "companies" ("firm_name","activity") VALUES
('MyFavouriteCustomer','Buy and pay');
INSERT INTO "customers" ("id","seller_id","note") VALUES (2,1,'customer
since 1966');

INSERT INTO "invoices" (seller_id,customer_id,invoice_no,invoice_date)
values (1,2,1,'now');

ERROR: <unnamed> referential integrity violation - key referenced from
invoices not found in customers

select * from customers;
id | seller_id | note
----+-----------+---------------------
2 | 1 | customer since 1816
(1 row)

-------

Why have I found this ERROR about referential integrity violation, if
the record to reference is
in the customer table?

Thank you in advance, \fer

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Christopher Sawtell 2001-01-28 10:47:18 Re: Re: Problem with Dates
Previous Message Joe Conway 2001-01-28 05:40:27 current host and dbname info