Skip to main content
Drizzle ORM provides various utility functions for working with MySQL schemas, tables, and query building.

Schema Utilities

getTableConfig()

Extracts complete configuration from a table definition.
MySqlTable
required
The table to extract configuration from.

Examples


getViewConfig()

Extracts configuration from a view definition.
MySqlView
required
The view to extract configuration from.

Examples


Index Utilities

index()

Creates an index definition.
string
The name of the index. If not provided, a name will be generated.

Examples


uniqueIndex()

Creates a unique index definition.

Examples


primaryKey()

Defines a composite primary key.
MySqlColumn[]
required
Array of columns that form the composite primary key.

Examples


foreignKey()

Defines a foreign key constraint.
MySqlColumn[]
required
Columns in the current table.
MySqlColumn[]
required
Referenced columns in the foreign table.
string
Name for the foreign key constraint.

Examples


unique()

Defines a unique constraint.

Examples


check()

Defines a check constraint.
string
required
Name of the check constraint.
SQL
required
The check condition as a SQL expression.

Examples


Query Utilities

QueryBuilder

Helper class for building queries programmatically.

sql

Template tag for raw SQL expressions.

sql.placeholder()

Creates a placeholder for prepared statements.

Examples


sql.raw()

Creates a SQL expression from a raw string.
Use sql.raw() with caution. It bypasses parameter binding and can be vulnerable to SQL injection if used with user input.

Examples


Type Utilities

InferSelectModel

Infers the TypeScript type for SELECT operations.

InferInsertModel

Infers the TypeScript type for INSERT operations.

Helper Functions

extractUsedTable()

Extracts table names from a table reference, subquery, or SQL expression.

Examples


convertIndexToString()

Converts index builders or names to string array.

Examples


Column Helpers

These functions are available on column builders:

$type()

Overrides the inferred TypeScript type.

$default()

Sets a runtime default value.

$onUpdate()

Sets a runtime update value.

Alias Utilities

alias()

Creates an alias for a table, useful for self-joins.

Migration Utilities

These utilities are used internally by the migration system but can be useful for custom migration tools:

getTableName()

Gets the fully qualified table name.

Connection Utilities

withReplicas()

Configures read replicas for the database connection. See Database documentation for details.

Best Practices

Use typed models: Always use InferSelectModel and InferInsertModel to maintain type safety throughout your application.
Parameterized queries: Use the sql template tag with interpolation instead of sql.raw() to prevent SQL injection.
Raw SQL: Only use sql.raw() with static strings or trusted input. Never use it with user-provided data.
Schema introspection: Use getTableConfig() to programmatically inspect table definitions for documentation, validation, or code generation.