Given:
CREATE TABLE operation
(
id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY;
);
CREATE TABLE listing
(
id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
operation_id BIGINT REFERENCES operation (id)
);
CREATE TABLE listing_card
(
id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
listing_id BIGINT REFERENCES listing (id),
listing_operation_id BIGINT REFERENCES operation (id),
index SMALLINT NOT NULL,
UNIQUE (listing_operation_id, index)
);
- Multiple listings may reference the same operation.
How do I ensure that table listing contains a (id, operation_id) combination that matches listing_card's (listing_id, listing_operation_id)?
I want something along the lines of FOREIGN KEY (listing_id, listing_operation_id) REFERENCES listing (id, operation_id) but I cannot do this because (id, operation_id) is not unique (multiple listings could reference the same operation).
Operations are referenced by different kinds of tables. The ultimate goal of the above schema is to ensure that listing_card.index is unique across any listing operations. A single operation may be referenced by 3 different listings, but their card indexes must be unique across each other (across the entire operation).
UNIQUEconstraint onlisting (id, operation_id)I was able to add the correspondingFOREIGN KEYtolisting_card. – Gili Jun 12 '22 at 03:28listing_card.listing_operation_idas you suggested, I wouldn't be able to enforce the cross-table constraintUNIQUE (listing_operation_id, index), right? So I don't think this column is redundant. – Gili Jun 13 '22 at 03:11listing_operation_id. So you need the column. Removing my misguided addition again. – Erwin Brandstetter Jun 13 '22 at 03:39