netspective-labs / sql-aide

SQL Aide (SQLa) Typescript template literal text supplier optimized for emitting SQL
https://www.sql-aide.com/
MIT License
16 stars 17 forks source link

Automated SQL CHECK Constraint Generation #178

Open alan-francis opened 11 months ago

alan-francis commented 11 months ago

I would like to propose an enhancement to the library to automate the generation of SQL CHECK constraints, taking into consideration existing unique constraints. Currently, users manually specify CHECK constraints when creating tables, which can be cumbersome and prone to errors.

I suggest incorporating a feature into the library that simplifies the definition of SQL CHECK constraints. Users should be able to express constraints more abstractly, specifying the allowed values for a column without having to write the entire CHECK constraint manually.

Consider the following SQL table creation script with both UNIQUE and CHECK constraints:


CREATE TABLE "synthetic_table_with_constraints" (
    "text_primary_key" TEXT PRIMARY KEY NOT NULL,
    "column_unique" TEXT NOT NULL,
    "column_check" TEXT NOT NULL CHECK (column_check IN('ACTIVE','INACTIVE')),
    "created_at" DATE,
    UNIQUE("column_unique")
);