{"success":true,"name":"let-sql-constraints-guard-data-not-just-app-code","description":"Enforcing data rules in SQL only through application checks.","category":"ai-brimind","path":"ai-brimind/let-sql-constraints-guard-data-not-just-app-code/SKILL.md","file":"SKILL.md","content":"---\nname: let-sql-constraints-guard-data-not-just-app-code\ndescription: \"Enforcing data rules in SQL only through application checks.\"\n---\n\n# Let SQL Constraints Guard Data, Not Just App Code\n\n## When to use\nProject uses a SQL database, and you are adding validation like 'this must be unique' or 'this can't be empty' only in application code.\n\n## Example request\nUniqueness checked with a SELECT before INSERT → two requests pass the check at once; a UNIQUE constraint closes the race. Required field validated in the form → a script or migration writes NULL anyway; NOT NULL is the real gate. Orphan records cleaned by a cron job → FOREIGN KEY with ON DELETE would prevent them existing; prevention beats cleanup. Status limited by an if-statement → a typo status still gets stored; a CHECK constraint or enum rejects it at the source.\n\n## Instructions\nAct like a data guardian who assumes application code is only one of many writers to the database. For each data rule you're about to enforce: name it (uniqueness, presence, referential integrity, allowed values) and check whether SQL has a built-in constraint for it — UNIQUE, NOT NULL, FOREIGN KEY, CHECK, DEFAULT. Put the rule in the schema as the source of truth, and keep app-level validation only for friendly error messages. If you're unsure whether a constraint is safe to add to existing data, query for violating rows first instead of guessing. Only proceed once the rule lives in the database itself, and explain why bad data is now impossible to store, not just unlikely.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1025\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}