Expand description
Generic DDL: turn an Entity’s metadata into CREATE TABLE +
CREATE INDEX statements, rendered for any Dialect.
The same metadata the derive produces for CRUD also describes the schema —
columns, a primary key, single-column UNIQUEs, foreign keys (inline), and
secondary indexes (separate statements). So callers can stand up the
backing schema for a struct without hand-writing a migration — handy for
tests, ephemeral D1 databases, and bootstrap. Production migrations can
still be authored explicitly; this is the convenience path.
Use schema_ddl to get every statement in dependency order (table first,
then its indexes); create_table_ddl returns just the table.
Foreign keys & SQLite/D1. FK clauses are emitted inline in the
CREATE TABLE (the only form SQLite accepts). SQLite/D1 do not enforce
them unless PRAGMA foreign_keys = ON is set on the connection — run that
once after connecting if you want enforcement.
Functions§
- apply_
type - Apply the column’s SQL type to a sea-query
ColumnDef. sea-query maps each of these to the per-dialect concrete type when rendered (e.g.BigUnsigned→BIGINT UNSIGNEDon MySQL,INTEGERon SQLite). - create_
index_ ddls - Render
E’s secondary indexes asCREATE [UNIQUE] INDEXstatements fordialect, one string each. - create_
table_ ddl - Render
E’sCREATE TABLE(without secondary indexes) as SQL fordialect. - create_
table_ stmt - Build the
CREATE TABLEstatement forE— columns, primary key, single-column uniques, and inline foreign keys. Secondary indexes are separate; seecreate_index_ddls. - schema_
ddl - Every DDL statement to create
E’s schema, in dependency order: the table (with its inline uniques and foreign keys) first, then each secondary index. Execute them in order.