Application schema definition
Create a typed table model for application queries.
Drizzle ORM · Function
mysqlTable declares a typed MySQL table schema in TypeScript. It combines a table name with MySQL column builders and optional table-level configuration for use in Drizzle queries and migrations.
mysqlTable creates a Drizzle schema object for a MySQL table. Pass the database table name and a column-definition object; each column uses a MySQL type builder that can be configured with constraints and defaults.
mysqlTable(name, columns, extraConfig?)| Name | Type | Description |
|---|---|---|
name | string | Database table name. |
columns | Record<string, MySqlColumnBuilder> | Object containing column definitions and their MySQL type-builder configuration. |
extraConfig | (table) => MySqlTableExtraConfig | undefined | Optional table-level configuration, including supported indexes and constraints. |
A typed Drizzle MySQL table schema object used in queries and migrations.
schema.ts file or be organized across multiple files.mysqlTable from drizzle-orm/mysql-core; Drizzle does not provide one shared table constructor.import { int, mysqlTable, varchar } from "drizzle-orm/mysql-core";
export const users = mysqlTable("users", {
id: int().primaryKey().autoincrement(),
name: varchar({ length: 256 }).notNull(),
age: int().notNull(),
});Defines a typed MySQL table schema with an auto-incrementing primary key and required columns.
Create a typed table model for application queries.
Generate and apply database changes from TypeScript declarations.
Express MySQL-specific columns and constraints with builder APIs.
mysqlTable do?mysqlTable declares a MySQL table in TypeScript and returns a typed table object used by Drizzle queries and migrations. Its first argument is the database table name, and its second argument describes columns and their configuration.
Follow-up: How would you add a multi-column constraint? Use the optional third table argument to define table-level configuration, such as indexes or unique constraints, when supported by the MySQL schema API.
mysqlTable?A column definition is created with a MySQL type builder such as int() or varchar({ length: 256 }). Builder methods such as primaryKey(), autoincrement(), notNull(), and unique() refine the schema and its inferred types.
Follow-up: Why use mysqlTable instead of a generic table helper? The schema must match the selected SQL dialect, including its native column types and constraints.
mysqlTable instead of writing only SQL?The declared schema acts as the source of truth for Drizzle query types and migration generation. You can keep declarations in one schema file or distribute them across files, provided they are included in the configured schema.
Follow-up: What limitation applies to the declaration? A table must contain at least one column, just as a database table does.
intvarchartextindexuniqueDrizzle Kitdrizzle-orm/mysql-core