Skip to main content
doc0blade47/drizzle-ormdocumentation home
Sign in

Dialects And Schema Schema Definition

Last updated Oct 12, 2026100% grounded
Relevant source files

The following files were used as context for generating this wiki page:

Overview

The schema definition layer in Drizzle ORM provides a type-safe, declarative abstraction for structuring database tables, columns, constraints, indexes, sequences, and security primitives across different database engines (PostgreSQL, MySQL, SQLite, SingleStore, and Gel). By utilizing fluent method chaining and TypeScript type inference, Drizzle core modules allow developers to map database catalogs directly to strongly typed models without sacrificing native SQL control. This architecture bridges the gap between raw DDL statements and application code, powering migration generation via Migration Toolkit Schema and database reverse engineering via Database Introspection.


Gel Schema Definition

Gel schema definitions are handled via the gel-core package, mimicking PostgreSQL-like semantics while supporting Gel-specific types and structures. Tables are declared using gelTable or via GelSchema.table() instances, tracking columns, foreign keys, and indexes in a unified metadata structure.

Sources: drizzle-orm/src/gel-core/table.ts:73-141

The following call chain demonstrates how Gel tables and their columns are initialized and built during schema parsing: gelTable() → gelTableWithSchema() → getGelColumnBuilders() → columnBuilder.build()

Sources: drizzle-orm/src/gel-core/columns/all.ts:23-46

Sources: drizzle-orm/src/gel-core/table.ts:249-251

import { gelTable, integer, text, index, check } from '~/gel-core/index.ts';
 
export const users = gelTable(
  'users_table',
  {
    id: integer('id').primaryKey(),
    age: integer('age1').notNull(),
    className: text('class').notNull(),
  },
  (users) => [
    index('usersAgeIdx').on(users.age.asc().nullsFirst()),
    check('legalAge', sql`${users.age} > 18`),
  ]
);

Sources: drizzle-orm/type-tests/geldb/tables.ts:65-98


MySQL Schema Definition

MySQL schema definitions reside in mysql-core, offering support for auto-incrementing columns, storage engines, table-level constraints, and character sets. Tables are initialized via mysqlTable or mysqlDatabase().table().

Sources: drizzle-orm/src/mysql-core/table.ts:61-116

Columns supporting auto-increment extend MySqlColumnBuilderWithAutoIncrement, enabling the .autoincrement() modifier which automatically flags the column as having a default value in the Drizzle type system.

Sources: drizzle-orm/src/mysql-core/columns/common.ts:128-145

Index configurations for MySQL utilize IndexBuilderOn to manage column lists and algorithms.

Sources: drizzle-orm/src/mysql-core/indexes.ts:34-42

import { mysqlTable, int, varchar, index } from '~/mysql-core/index.ts';
 
export const users = mysqlTable(
  'users',
  {
    id: int('id').primaryKey().autoincrement(),
    email: varchar('email', { length: 255 }).notNull().unique(),
  },
  (table) => [
    index('email_idx').using('btree').on(table.email),
  ]
);

Sources: drizzle-orm/src/mysql-core/table.ts:119-147


PostgreSQL Schema Definition

Column Builders and Extensions

PostgreSQL schema definitions utilize pg-core and provide extensive capabilities including standard types, PostGIS extensions, pgvector embeddings, sequences, check constraints, unique constraints, and row-level security (RLS) policies. PostgreSQL supports complex structural column builders such as geometry for spatial data and vector for machine learning embeddings. When defining vectors, specifying an operator class (such as vector_cosine_ops or vector_l2_ops) during index creation is required by pgvector.

Sources: drizzle-orm/src/pg-core/columns/postgis_extension/geometry.ts:105-119, drizzle-orm/src/pg-core/columns/vector_extension/vector.ts:70-79

import { pgTable, serial } from '~/pg-core/index.ts';
import { geometry } from '~/pg-core/columns/postgis_extension/geometry.ts';
import { vector } from '~/pg-core/columns/vector_extension/vector.ts';
 
export const items = pgTable('items', {
  id: serial('id').primaryKey(),
  location: geometry('location', { srid: 4326, mode: 'xy' }),
  embedding: vector('embedding', { dimensions: 1536 }),
});

Sources: drizzle-orm/type-tests/pg/tables.ts:1312-1335

Indexes and Row-Level Security

Row-Level Security (RLS) can be enabled on tables using the .enableRLS() method chain, and security policies are attached using pgPolicy.

Sources: drizzle-orm/src/pg-core/table.ts:126-136, drizzle-orm/src/pg-core/policies.ts:35-52

import { pgTable, text, integer, index, pgPolicy } from '~/pg-core/index.ts';
import { sql } from '~/sql/sql.ts';
 
export const documents = pgTable(
  'documents',
  {
    id: integer('id').primaryKey(),
    ownerId: text('owner_id').notNull(),
  },
  (table) => [
    index('owner_idx').using('btree', table.ownerId.asc()),
    pgPolicy('user_isolation_policy', {
      for: 'all',
      to: 'public',
      using: sql`owner_id = current_user`,
    }),
  ]
).enableRLS();

Sources: drizzle-orm/src/pg-core/indexes.ts:159-178


SingleStore Schema Definition

SingleStore schemas map distributed relational database capabilities using singlestoreSchema and singlestoreTable.

Sources: drizzle-kit/src/introspect-singlestore.ts:10-13

Introspection routines convert database column defaults, JSON types, and indexes into corresponding Drizzle schema declarations.

Sources: drizzle-kit/src/introspect-singlestore.ts:139-156

import { singlestoreTable, int, varchar } from 'drizzle-orm/singlestore-core';
 
export const storeItems = singlestoreTable('store_items', {
  id: int('id').primaryKey().autoincrement(),
  title: varchar('title', { length: 128 }).notNull(),
});

Sources: drizzle-kit/src/introspect-singlestore.ts:19-55


SQLite Schema Definition

SQLite schema definitions leverage lightweight type mappings suitable for embedded and edge runtimes (such as Cloudflare D1 or Expo SQLite).

Sources: drizzle-kit/src/sqlgenerator.ts:117-124

They handle primary keys, unique constraints, and foreign key references through sqlite-core.

Sources: drizzle-kit/src/sqlgenerator.ts:125-142

Was this page helpful?