Database Metadata
Usestatic schema and static database for different responsibilities.
static schema- columns
- validation
- runtime relation semantics through
relation(...)
static database- foreign keys
- composite unique indexes
- composite normal indexes
- named database constraints the ORM must generate or preserve
schemastays the application and runtime model contractdatabasestays the physical SQL contract
Timestamp rule
Timestamp columns still belong instatic schema.
Use static database for relational DDL only, and keep timestamp semantics explicit in the schema:
useTz: truewhen PostgreSQL must emitTIMESTAMPTZdefaultNow: falsefor business timestamps that must stay empty until the application sets themsoftDeletesshould never auto-default to the current time
Why this exists
relation(...) describes model intent, but not every relational database rule should be inferred from runtime relation metadata alone.
Examples:
- a named PostgreSQL foreign key
- a composite unique index
- a composite lookup index used for auth, sessions, or idempotency
- a safe smart-update diff that must preserve unmanaged database constraints
static database.
Example
Clean usage rule
Use this rule consistently:- put simple scalar columns in
static schema - add
relation(...)only when the model needs runtime relation behavior - put physical database enforcement in
static database
static database.
Do not rely on relation(...) alone for critical database enforcement.
Relation helpers still matter
Core relation helpers are still the runtime relation contract:belongsToMany implies a pivot table. Helper migrations for composite indexes or relational extras are not pivot tables and should be named by target table or purpose.
Model-first migration flow
Follow this order:- update the model
- generate migrations with
eloquent make:migration - run migrations
- rerun generation and expect no schema differences
Safe smart-update behavior
The ORM tracksstatic database metadata during smart-update diffs.
That means:
- ORM-managed foreign keys and indexes can be updated cleanly
- unmanaged database constraints are preserved by default
- the diff engine should not generate destructive cleanup just because a live database has extra relational DDL the model does not own