---
title: "Dialects And Schema Schema Definition"
description: "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 di..."
last_updated: "2026-10-12T08:14:00.734644+00:00"
canonical_url: "https://www.doc0.dev/docs/e1b68fed-3c4e-4c95-b2ba-ebf050f78025/technical/dialects-and-schema/dialects-and-schema-schema-definition"
---

<details>
<summary>Relevant source files</summary>

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

- [drizzle-kit/src/introspect-gel.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-kit/src/introspect-gel.ts)
- [drizzle-orm/type-tests/pg/tables.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/type-tests/pg/tables.ts)
- [drizzle-kit/src/serializer/pgSerializer.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-kit/src/serializer/pgSerializer.ts)
- [drizzle-kit/src/introspect-pg.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-kit/src/introspect-pg.ts)
- [drizzle-kit/src/sqlgenerator.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-kit/src/sqlgenerator.ts)
- [drizzle-kit/src/serializer/pgSchema.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-kit/src/serializer/pgSchema.ts)
- [drizzle-kit/src/serializer/gelSerializer.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-kit/src/serializer/gelSerializer.ts)
- [drizzle-orm/type-tests/geldb/tables.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/type-tests/geldb/tables.ts)
- [drizzle-kit/src/serializer/gelSchema.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-kit/src/serializer/gelSchema.ts)
- [drizzle-kit/src/introspect-singlestore.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-kit/src/introspect-singlestore.ts)
- [drizzle-orm/src/pg-core/indexes.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/pg-core/indexes.ts)
- [drizzle-orm/src/pg-core/dialect.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/pg-core/dialect.ts)
- [drizzle-orm/src/gel-core/indexes.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/gel-core/indexes.ts)
- [drizzle-orm/src/pg-core/columns/common.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/pg-core/columns/common.ts)
- [drizzle-orm/src/pg-core/table.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/pg-core/table.ts)
- [drizzle-orm/src/pg-core/index.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/pg-core/index.ts)
- [drizzle-orm/src/gel-core/columns/all.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/gel-core/columns/all.ts)
- [drizzle-orm/src/gel-core/columns/common.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/gel-core/columns/common.ts)
- [drizzle-orm/src/gel-core/schema.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/gel-core/schema.ts)
- [drizzle-orm/src/gel-core/table.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/gel-core/table.ts)
- [drizzle-orm/src/mysql-core/columns/all.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/mysql-core/columns/all.ts)
- [drizzle-orm/src/mysql-core/columns/common.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/mysql-core/columns/common.ts)
- [drizzle-orm/src/mysql-core/foreign-keys.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/mysql-core/foreign-keys.ts)
- [drizzle-orm/src/mysql-core/indexes.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/mysql-core/indexes.ts)
- [drizzle-orm/src/mysql-core/schema.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/mysql-core/schema.ts)
- [drizzle-orm/src/mysql-core/table.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/mysql-core/table.ts)
- [drizzle-orm/src/pg-core/columns/all.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/pg-core/columns/all.ts)
- [drizzle-orm/src/pg-core/columns/postgis_extension/geometry.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/pg-core/columns/postgis_extension/geometry.ts)
- [drizzle-orm/src/pg-core/columns/vector_extension/vector.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/pg-core/columns/vector_extension/vector.ts)
- [drizzle-orm/src/pg-core/policies.ts](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/pg-core/policies.ts)
</details>

## 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](https://www.doc0.dev/docs/e1b68fed-3c4e-4c95-b2ba-ebf050f78025/technical/migration-toolkit/migration-toolkit-schema) and database reverse engineering via [Database Introspection](https://www.doc0.dev/docs/e1b68fed-3c4e-4c95-b2ba-ebf050f78025/technical/migration-toolkit/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](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/gel-core/table.ts#L73-L141)

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](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/gel-core/columns/all.ts#L23-L46)

```mermaid
sequenceDiagram
    participant TableDef as gelTable
    participant TableWithSchema as gelTableWithSchema
    participant Builders as getGelColumnBuilders
    participant ColumnBuilder as GelColumnBuilder
    participant Column as GelColumn

    TableDef->>TableWithSchema: gelTable(name, columns, extraConfig)
    TableWithSchema->>Builders: getGelColumnBuilders()
    Builders-->>TableWithSchema: Returns column builder map
    TableWithSchema->>ColumnBuilder: setName(name)
    ColumnBuilder->>Column: build(rawTable)
    Column-->>TableWithSchema: Returns built column instance
```

Sources: [drizzle-orm/src/gel-core/table.ts:249-251](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/gel-core/table.ts#L249-L251)

```typescript
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](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/type-tests/geldb/tables.ts#L65-L98)

---

## 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](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/mysql-core/table.ts#L61-L116)

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](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/mysql-core/columns/common.ts#L128-L145)

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

Sources: [drizzle-orm/src/mysql-core/indexes.ts:34-42](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/mysql-core/indexes.ts#L34-L42)

```typescript
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](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/mysql-core/table.ts#L119-L147)

---

## 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](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/pg-core/columns/postgis_extension/geometry.ts#L105-L119), [drizzle-orm/src/pg-core/columns/vector_extension/vector.ts:70-79](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/pg-core/columns/vector_extension/vector.ts#L70-L79)

```typescript
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](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/type-tests/pg/tables.ts#L1312-L1335)

### 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](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/pg-core/table.ts#L126-L136), [drizzle-orm/src/pg-core/policies.ts:35-52](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/pg-core/policies.ts#L35-L52)

```typescript
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](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-orm/src/pg-core/indexes.ts#L159-L178)

---

## SingleStore Schema Definition

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

Sources: [drizzle-kit/src/introspect-singlestore.ts:10-13](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-kit/src/introspect-singlestore.ts#L10-L13)

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

Sources: [drizzle-kit/src/introspect-singlestore.ts:139-156](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-kit/src/introspect-singlestore.ts#L139-L156)

```typescript
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](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-kit/src/introspect-singlestore.ts#L19-L55)

---

## 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](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-kit/src/sqlgenerator.ts#L117-L124)

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

Sources: [drizzle-kit/src/sqlgenerator.ts:125-142](https://github.com/blade47/drizzle-orm/blob/48e5406027103a9fca6eb66417187c4a8b5c6aa3/drizzle-kit/src/sqlgenerator.ts#L125-L142)

## Related

- [Dialects And Schema Query Builders](https://www.doc0.dev/docs/e1b68fed-3c4e-4c95-b2ba-ebf050f78025/technical/dialects-and-schema/dialects-and-schema-query-builders)
- [Table and Column Models](https://www.doc0.dev/docs/e1b68fed-3c4e-4c95-b2ba-ebf050f78025/technical/core-architecture/table-and-column-models)
- [Drivers And Runtimes](https://www.doc0.dev/docs/e1b68fed-3c4e-4c95-b2ba-ebf050f78025/technical/drivers-and-runtimes/drivers-and-runtimes)
- [Edge and Mobile SQLite](https://www.doc0.dev/docs/e1b68fed-3c4e-4c95-b2ba-ebf050f78025/technical/drivers-and-runtimes/edge-and-mobile-sqlite)


## Sitemap

See the full [sitemap](https://www.doc0.dev/docs/e1b68fed-3c4e-4c95-b2ba-ebf050f78025/llms.txt) for all pages in this wiki.
