D1 supports a number of database-level commands that allow you to list tables, indexes, and inspect the schema for a given table or index.
Database statements
D1 supports a number of database-level statements that allow you to list tables, indexes, and inspect the schema for a given table or index.
You can execute any of these statements via the D1 console in the Cloudflare dashboard, wrangler d1 execute, or with the D1 client API.
PRAGMA table_list
Lists the tables in the database. This includes the system tables maintained by D1.
PRAGMA table_info(TABLE_NAME)
Shows the schema (columns, types, null, default values) for the given TABLE_NAME.
PRAGMA table_xinfo(TABLE_NAME)
Similar to PRAGMA table_info(TABLE_NAME) but also includes generated columns.
PRAGMA index_list(TABLE_NAME)
Show the indexes for the given TABLE_NAME.
PRAGMA index_info(INDEX_NAME)
Show the indexed column(s) for the given INDEX_NAME.
Query sqlite_master
You can also query the sqlite_master table to show all tables, indexes, and the original SQL used to generate them:
PRAGMA defer_foreign_keys = (on|off)
Allows you to defer the enforcement of foreign key constraints until the end of the current transaction. This can be useful during database migrations, as schema changes may temporarily violate constraints depending on order in which they are applied.
This does not disable foreign key enforcement outside of the current transaction: if you have not resolved outstanding foreign key violations at the end of your transaction, it will fail with a FOREIGN KEY constraint failed error.
Note that setting PRAGMA defer_foreign_keys = ON does not prevent ON DELETE CASCADE actions from being executed. While foreign key constraint checks are deferred until the end of a transaction, ON DELETE CASCADE operations will remain active, consistent with SQLite’s behavior.
To defer foreign key enforcement, set PRAGMA defer_foreign_keys = on at the start of your transaction, or ahead of changes that would violate constraints: