Getting Started
Configuration
Troubleshooting
Declaring your database schema allows you to define the structure of your data—such as tables, columns, and constraints—directly within your project. This approach provides a clear, type-safe blueprint for how your application interacts with the database.
By using these declarations, you ensure that your code stays in sync with your database structure, enabling features like automatic type inference and clearer query building.
To define a table, you use a table-creator function specific to your database type (mysqlTable, pgTable, or sqliteTable).
import { mysqlTable, int, varchar } from 'drizzle-orm/mysql-core';
export const users = mysqlTable('users', {
id: int('id').primaryKey(),
fullName: varchar('full_name', { length: 256 }),
});int(), varchar()) used to set the data type and properties for each field in your table.Tip
When defining extra configurations like indexes or constraints, always pass them as an array in the third parameter. This is the modern, preferred syntax.
You can add indexes, foreign keys, or unique constraints to a table using the third parameter. This is useful when a constraint spans multiple columns or requires specific settings.
import { mysqlTable, int, index } from 'drizzle-orm/mysql-core';
export const users = mysqlTable('users', {
id: int('id'),
cityId: int('city_id'),
}, (table) => [
index('city_idx').on(table.cityId),
]);Warning
While older versions allowed passing an object to the third parameter, this is now deprecated. Please use the array-based syntax shown above to ensure compatibility with future updates.