db-schema-validator
Skill: db-schema-validator
Purpose
Validate and enforce a database schema that is:
- MySQL-compatible
- production-ready
- aligned with OpenAPI
- aligned with planner business rules
- safe for module-based backend generation
Required Table Structure
Every business table must include:
id BIGINT PRIMARY KEY AUTO_INCREMENTcreated_at DATETIME NOT NULLupdated_at DATETIME NOT NULLdeleted_at DATETIME NULL
These are mandatory unless there is a clearly approved exception.
Naming Rules
Tables
- snake_case only
- plural preferred where appropriate
Columns
- snake_case only
Forbidden:
- camelCase in DB
- mixed naming styles
Primary Key Rules
Every business table must:
- have a primary key
- use BIGINT for primary IDs
- use AUTO_INCREMENT unless another strategy is explicitly justified
Forbidden:
- missing PK
- text-based primary keys by accident
- inconsistent ID strategy without reason
Foreign Key Rules
All real relationships must be explicit.
Requirements:
- define FK constraints
- reference the correct parent table
- index all FK columns
For many-to-many:
- use join tables
- index both FK sides
Forbidden:
- implied-only relationships
- missing FK on real ownership relations
- FK without index
Index Rules
Must include:
- index on FK columns
- UNIQUE index on truly unique fields
- search/filter indexes when implied by list/search APIs
- optional composite indexes for common list/filter patterns
Common examples:
emailcodeslug(status, created_at)(user_id, created_at)
Data Type Rules
Use clear, appropriate types:
id->BIGINT- normal string ->
VARCHAR(255) - long text ->
TEXT - status ->
VARCHAR(50) - money/price ->
DECIMAL(10,2) - datetime ->
DATETIME
Forbidden:
- using
TEXTfor everything - oversized strings without reason
- ambiguous numeric types for money
Business Rule Mapping
Planner/OpenAPI rules must map into DB schema:
- required ->
NOT NULL - unique ->
UNIQUEconstraint or index - enum/status -> controlled
VARCHAR(50)or clearly defined DB enum strategy - soft delete ->
deleted_at - ownership -> FK
- list/search/filter use cases -> indexes
API Alignment Rule
Every critical API field must map to a DB column.
The schema must align with:
- planner output
- OpenAPI field names and types
- backend entity mapping needs
Do not add critical hidden fields unless clearly justified as internal.
Soft Delete Rule
Soft delete must be implemented with:
deleted_at DATETIME NULL
Normal business reads should assume:
- active rows ->
deleted_at IS NULL
Hard delete must never be the default unless explicitly required.
Normalization Rule
The schema should be reasonably normalized.
Avoid:
- storing repeated structured data in flat duplicated columns
- denormalized design without clear performance reason
- mixing unrelated business concepts into one table
Quality Checklist
Before accepting schema:
- all business tables have PK
- required base columns exist
- all real relationships have FK
- all FK columns are indexed
- unique rules are enforced
- search/list behavior has index support
- OpenAPI-critical fields exist
- MySQL syntax is valid
Forbidden Patterns
- no primary key
- missing foreign key
- no index on foreign key
- no deleted_at where soft delete is expected
- weak alignment with API/planner
- using DB naming that breaks clean entity mapping
Final Goal
The final schema must be:
- safe
- scalable
- consistent
- query-friendly
- ready for backend generation
