SeaQL / sea-query

🔱 A dynamic SQL query builder for MySQL, Postgres and SQLite
https://www.sea-ql.org
Other
1.12k stars 182 forks source link

`CHECK` constraint with a name #780

Open xamgore opened 3 months ago

xamgore commented 3 months ago

Motivation

Having a schema with defined constraint checks, I'd like to see a meaningful error message.

.check(Expr::col(Products::Price).gt(0))

Without it, all we'll get is:

⛔️ new row for relation "products" violates check constraint "products_check".

Proposed Solutions

Extend the API in a way it could introduce names for constraints.

CREATE TABLE products (
    product_no integer,
    name text,
    price numeric CONSTRAINT positive_price CHECK (price > 0)
--                           ^^^^^^^^^^^^^^
);

Matching error messages against those "positive_price" names allows translating them for end users.