From ccc29d1954a2c95b87ed2047ce9a57e70f9c645e Mon Sep 17 00:00:00 2001 From: "bonk[bot]" Date: Wed, 18 Feb 2026 14:36:26 -0500 Subject: [PATCH 1/2] add SQLite STRICT tables documentation --- src/content/docs/sqlite-strict.mdx | 102 +++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/content/docs/sqlite-strict.mdx diff --git a/src/content/docs/sqlite-strict.mdx b/src/content/docs/sqlite-strict.mdx new file mode 100644 index 00000000..89f4733b --- /dev/null +++ b/src/content/docs/sqlite-strict.mdx @@ -0,0 +1,102 @@ +import Callout from '@mdx/Callout.astro'; +import CodeTabs from "@mdx/CodeTabs.astro"; + +# SQLite STRICT Tables + +SQLite STRICT tables enforce column type affinity at the database level. When a table is created with the `STRICT` keyword, SQLite will reject any attempt to insert or update a value that doesn't match the declared column type, rather than silently coercing the value. + +## Enable STRICT mode + +Use the `.strict()` method to create a STRICT table: + +```ts +import { sqliteTable, int, text } from 'drizzle-orm/sqlite-core'; + +export const users = sqliteTable('users', { + id: int('id').primaryKey(), + name: text('name').notNull(), + age: int('age'), +}).strict(); +``` + +This generates: + +```sql +CREATE TABLE `users` ( + `id` integer PRIMARY KEY NOT NULL, + `name` text NOT NULL, + `age` integer +) STRICT; +``` + +## Valid column types + +STRICT tables only allow these column types: + +| Type | Description | +| :--- | :---------- | +| `INT` or `INTEGER` | Signed integer | +| `REAL` | Floating point | +| `TEXT` | Text string | +| `BLOB` | Binary data | +| `ANY` | Any data type (no type enforcement) | + + +If you use a column type that isn't in this list (like `VARCHAR` or `BOOLEAN`), drizzle-kit will warn you during migration generation, and SQLite will reject the CREATE TABLE statement at runtime. + + +## Type enforcement behavior + +In a regular SQLite table, type affinity is flexible: + +```ts +// Regular table - SQLite accepts this and stores "hello" as text in an integer column +await db.execute(sql`INSERT INTO users (age) VALUES ('hello')`); +``` + +In a STRICT table, this would fail: + +```ts +// STRICT table - SQLite rejects this with an error +await db.execute(sql`INSERT INTO users (age) VALUES ('hello')`); +// Error: cannot store TEXT value in INTEGER column +``` + +## Migrations + +When you add or remove `.strict()` from an existing table, Drizzle Kit will generate a table recreation migration. This is required because SQLite doesn't support `ALTER TABLE` to change the STRICT option. + +**Adding STRICT to existing table:** + +```ts +// Before +export const users = sqliteTable('users', { + id: int('id').primaryKey(), +}); + +// After +export const users = sqliteTable('users', { + id: int('id').primaryKey(), +}).strict(); +``` + +This generates a `recreate_table` migration that: +1. Creates a new table with STRICT +2. Copies data from the old table +3. Drops the old table +4. Renames the new table + +## Introspection + +When using `drizzle-kit pull` to introspect an existing database, Drizzle will detect STRICT tables and include `.strict()` in the generated schema. + +## When to use STRICT tables + +STRICT tables are useful when: +- You need strict type enforcement at the database level +- You want to catch type mismatches early rather than having SQLite silently coerce values +- You're building applications where data integrity is critical + + +STRICT tables were added in SQLite 3.37.0 (November 2021). Make sure your SQLite version supports this feature. + From e3742018e16858dc6c796011a9cca89af7d4f626 Mon Sep 17 00:00:00 2001 From: "bonk[bot]" Date: Wed, 18 Feb 2026 14:41:42 -0500 Subject: [PATCH 2/2] add SQLite STRICT tables to nav, trim docs --- src/content/docs/_meta.json | 1 + src/content/docs/sqlite-strict.mdx | 62 +++++++----------------------- 2 files changed, 15 insertions(+), 48 deletions(-) diff --git a/src/content/docs/_meta.json b/src/content/docs/_meta.json index 8907fd51..bb36d55c 100644 --- a/src/content/docs/_meta.json +++ b/src/content/docs/_meta.json @@ -61,6 +61,7 @@ ["schemas", "Schemas"], ["relations-v2", "Drizzle Relations"], ["rls", "Row-Level Security (RLS)"], + ["sqlite-strict", "SQLite STRICT Tables"], ["extensions", "Extensions"], "---", ["relations", "[OLD] Drizzle Relations"], diff --git a/src/content/docs/sqlite-strict.mdx b/src/content/docs/sqlite-strict.mdx index 89f4733b..d0ecc99e 100644 --- a/src/content/docs/sqlite-strict.mdx +++ b/src/content/docs/sqlite-strict.mdx @@ -1,13 +1,10 @@ import Callout from '@mdx/Callout.astro'; -import CodeTabs from "@mdx/CodeTabs.astro"; # SQLite STRICT Tables -SQLite STRICT tables enforce column type affinity at the database level. When a table is created with the `STRICT` keyword, SQLite will reject any attempt to insert or update a value that doesn't match the declared column type, rather than silently coercing the value. +SQLite [STRICT tables](https://www.sqlite.org/stricttables.html) enforce column type affinity at the database level - SQLite rejects values that don't match the declared column type instead of silently coercing them. -## Enable STRICT mode - -Use the `.strict()` method to create a STRICT table: +## Usage ```ts import { sqliteTable, int, text } from 'drizzle-orm/sqlite-core'; @@ -19,7 +16,7 @@ export const users = sqliteTable('users', { }).strict(); ``` -This generates: +Generates: ```sql CREATE TABLE `users` ( @@ -31,72 +28,41 @@ CREATE TABLE `users` ( ## Valid column types -STRICT tables only allow these column types: +STRICT tables only allow these types: | Type | Description | | :--- | :---------- | -| `INT` or `INTEGER` | Signed integer | +| `INT` / `INTEGER` | Signed integer | | `REAL` | Floating point | | `TEXT` | Text string | | `BLOB` | Binary data | -| `ANY` | Any data type (no type enforcement) | +| `ANY` | Allows any type (no enforcement) | -If you use a column type that isn't in this list (like `VARCHAR` or `BOOLEAN`), drizzle-kit will warn you during migration generation, and SQLite will reject the CREATE TABLE statement at runtime. +Using other types (like `VARCHAR` or `BOOLEAN`) will trigger a warning during migration generation and SQLite will reject the DDL at runtime. -## Type enforcement behavior +## Behavior -In a regular SQLite table, type affinity is flexible: +Regular SQLite tables allow type coercion: ```ts -// Regular table - SQLite accepts this and stores "hello" as text in an integer column +// Regular table - SQLite stores "hello" in an integer column await db.execute(sql`INSERT INTO users (age) VALUES ('hello')`); ``` -In a STRICT table, this would fail: +STRICT tables reject mismatched types: ```ts -// STRICT table - SQLite rejects this with an error +// STRICT table - throws error await db.execute(sql`INSERT INTO users (age) VALUES ('hello')`); // Error: cannot store TEXT value in INTEGER column ``` -## Migrations - -When you add or remove `.strict()` from an existing table, Drizzle Kit will generate a table recreation migration. This is required because SQLite doesn't support `ALTER TABLE` to change the STRICT option. - -**Adding STRICT to existing table:** - -```ts -// Before -export const users = sqliteTable('users', { - id: int('id').primaryKey(), -}); - -// After -export const users = sqliteTable('users', { - id: int('id').primaryKey(), -}).strict(); -``` - -This generates a `recreate_table` migration that: -1. Creates a new table with STRICT -2. Copies data from the old table -3. Drops the old table -4. Renames the new table - ## Introspection -When using `drizzle-kit pull` to introspect an existing database, Drizzle will detect STRICT tables and include `.strict()` in the generated schema. - -## When to use STRICT tables - -STRICT tables are useful when: -- You need strict type enforcement at the database level -- You want to catch type mismatches early rather than having SQLite silently coerce values -- You're building applications where data integrity is critical +`drizzle-kit pull` detects existing STRICT tables and generates `.strict()` in the schema. -STRICT tables were added in SQLite 3.37.0 (November 2021). Make sure your SQLite version supports this feature. +STRICT tables require SQLite 3.37.0+ (November 2021).