Note that there are some explanatory texts on larger screens.

plurals
  1. POinsert several row in order to satisfy a constraint
    primarykey
    data
    text
    <p>I have two table: deck(id) and card(deck,color,value)</p> <p>deck have those constraints:</p> <ul> <li><code>CHECK (fifty_two_cards_deck(id))</code></li> <li><p><code>PRIMARY KEY (id)</code></p> <p>CREATE FUNCTION fifty_two_cards_deck(deck integer) RETURNS boolean LANGUAGE sql STABLE STRICT AS $<em>$ SELECT COUNT(*)=52 FROM card WHERE deck=$1 $</em>$;</p></li> </ul> <p>and card have those constraints: </p> <ul> <li><code>FOREIGN KEY (deck) REFERENCES deck(id)</code></li> <li><code>PRIMARY KEY (deck, color, value)</code></li> </ul> <p>How can I insert a new deck?</p> <p>I tried this:</p> <pre><code>begin transaction; INSERT INTO "public"."deck" ("id") VALUES (nextval('deck_id_seq'::regclass)); INSERT INTO "public"."card" ("deck", "color", "value") VALUES ('1', enum_first(null::Suit), enum_first(null::Symbol)); end transaction </code></pre> <p>(i had edit <code>fifty_two_cards_deck</code> to be a <code>one_card_deck</code> for testing purpose) but I got this error:</p> <blockquote> <p>SQL error:</p> <p>ERROR: new row for relation "deck" violates check constraint "fifty_two_cards_deck"</p> <p>In statement: begin transaction; INSERT INTO "public"."deck" ("id") VALUES (nextval('deck_id_seq'::regclass)); INSERT INTO "public"."card" ("deck", "color", "value") VALUES ('1', enum_first(null::Suit), enum_first(null::Symbol));</p> <p>end transaction</p> </blockquote> <p>How can I solve this without removing the constraints?</p> <p>EDIT: solution</p> <p>thx to Magnus Hagander I got it working like this (after setting the foreign key deferrable):</p> <pre><code>begin transaction; SET CONSTRAINTS ALL DEFERRED; INSERT INTO "public"."deck-card" ("deck", "position", "color", "value") VALUES (1, 0, enum_first(null::suit), enum_first(null::Symbol)); INSERT INTO "public"."deck" ("id") VALUES (1); end transaction </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload