Skip to main content

Module schema

Module schema 

Source
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 UNSIGNED on MySQL, INTEGER on SQLite).
create_index_ddls
Render E’s secondary indexes as CREATE [UNIQUE] INDEX statements for dialect, one string each.
create_table_ddl
Render E’s CREATE TABLE (without secondary indexes) as SQL for dialect.
create_table_stmt
Build the CREATE TABLE statement for E — columns, primary key, single-column uniques, and inline foreign keys. Secondary indexes are separate; see create_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.