Objective
Every integrity rule that lives in application code is a rule each new service has to re-implement, and one of them eventually will not. A background job, a migration script, an admin tool, an LLM agent writing SQL — each is a chance to insert an order for a customer who does not exist.
From v2.0.0-ce these rules live in the schema and the engine enforces them for every client, including the ones your team has not written yet.
Read this before upgrading. Earlier versions accepted
FOREIGN KEYand table-levelUNIQUEdeclarations and never checked them. If you have been running on an older build, you may already hold rows that violate constraints you believe are active. v2.0 refuses new violations; it does not clean up existing data. Audit before you rely on it.
Step 1: A schema that states its own rules
-- Drop child first: the parent cannot be dropped while it is referenced.
DROP TABLE IF EXISTS recipe_ri_orders;
DROP TABLE IF EXISTS recipe_ri_customers;
CREATE TABLE recipe_ri_customers (
customer_id INT PRIMARY KEY,
email TEXT UNIQUE,
name TEXT
);
CREATE TABLE recipe_ri_orders (
order_id INT PRIMARY KEY,
customer_id INT REFERENCES recipe_ri_customers(customer_id),
total DOUBLE
);
Two rules are now facts about the data rather than hopes about the code: an email appears at most once, and every order belongs to a real customer.
Step 2: Seed two customers and one valid order
INSERT INTO recipe_ri_customers (customer_id, email, name) VALUES
(1, 'ada@example.com', 'Ada Lovelace'),
(2, 'bo@example.com', 'Bo Nilsson');
INSERT INTO recipe_ri_orders (order_id, customer_id, total) VALUES
(10, 1, 99.00);
Expected: both succeed. Order 10 points at customer 1, which exists.
Step 3: Ground truth — the orphan is refused
This is the statement that silently succeeded before v2.0:
INSERT INTO recipe_ri_orders (order_id, customer_id, total) VALUES (11, 99, 50.00);
-- Constraint violation: Foreign key violation:
-- 'recipe_ri_orders' has no matching row in 'recipe_ri_customers'
The statement FAILS, and that is the result. Customer 99 does not exist, so the order cannot.
Customer 99 does not exist, so the order cannot. The error names both tables, which is what you want at 3am.
Why these three are not
sqlblocks. Steps 3, 4 and 5 are supposed to fail, so they are shown as plain text with the engine's real error beneath them. The certification harness executes only```sqlblocks, and a recipe whose point is three refusals would otherwise fail certification forever. Paste them into your own session to see the refusals first-hand.
Step 4: The duplicate key is refused too
INSERT INTO recipe_ri_customers (customer_id, email, name) VALUES (3, 'ada@example.com', 'Ada Clone');
-- Constraint violation: Duplicate key:
-- row violates a UNIQUE or PRIMARY KEY constraint on table 'recipe_ri_customers'
FAILS.
A different primary key is not enough — email is declared UNIQUE, and that
is now checked. This is the constraint that stops two sign-up paths creating
the same account twice.