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

Dialects And Schema Query Builders

Last updated Oct 12, 2026

Overview

The Drizzle ORM query building architecture relies on dialect-specific compiler components coupled with fluent query builder classes to transform high-level method chains into parameterized SQL AST fragments. Each database dialect (PostgreSQL, Gel, SQLite, MySQL, and SingleStore) provides its own dialect driver class that manages identifier escaping, parameter formatting, casing cache transformations, and clause assembly for insert, update, delete, and select operations.


Gel Query Builders

The Gel query builder subsystem shares architectural lineages with PostgreSQL-core query builders, utilizing GelDialect, GelInsertBase, GelSelectBase, and GelUpdateBase to compile queries targeting Gel databases. Insert configurations handle column filtering via col.shouldDisableInsert(), fallback to column default functions (col.defaultFn) or onUpdateFn, and compile returning and on conflict clauses when present.

const inserted = await db.insert(users)
  .values({ name: 'Gel User' })
  .returning();

The Gel selection framework leverages GelSelectBuilder and GelSelectQueryBuilderBase to manage complex relation projections, join configurations, and set operators (union, intersect, except) with strict key validation checks.

Note

Gel query builder methods validate set operator compatibility by inspecting whether left and right selection keys match identically and maintain the same order.

if (!haveSameKeys((leftSelect as any).getSelectedFields(), setOperator.rightSelect.getSelectedFields())) {
    throw new Error(
        'Set operator error (union / intersect / except): selected fields are not the same or are in a different order',
    );
}

MySQL Query Builders

MySQL-core query compilation is managed by MySqlDialect alongside MySqlInsertConfig and related query builders. When compiling insert queries via buildInsertQuery, the dialect filters out insert-disabled columns and iterates over input value records.

	buildInsertQuery({
		table,
		values: valuesOrSelect,
		ignore,
		onConflict,
		select,
	}: MySqlInsertConfig): { sql: SQL; generatedIds: Record<string, unknown>[] } {

For each column entry during an insert operation, if a field value is undefined or an unassigned Param, the dialect checks column defaults (col.defaultFn) or update functions (col.onUpdateFn), populates generated ID tracking dictionaries, and constructs ignore and on duplicate key clauses.

Important

MySQL does not support a native do nothing conflict clause directly; developers achieve this behavior by setting a unique column to its own value (e.g., { id: sqlid }) within .onDuplicateKeyUpdate().

	onDuplicateKeyUpdate(
		config: SingleStoreInsertOnDuplicateKeyUpdateConfig<this>,
	): SingleStoreInsertWithout<this, TDynamic, 'onDuplicateKeyUpdate'> {
		const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
		this.config.onConflict = sql`update ${setSql}`;
		return this as any;
	}

PostgreSQL Query Builders

PostgreSQL dialect compilation (PgDialect) drives advanced clause generation including CTEs (withList), column casing identifiers, system value overrides (overriding system value), and conflict resolution.

	buildInsertQuery(
		{ table, values: valuesOrSelect, onConflict, returning, withList, select, overridingSystemValue_ }: PgInsertConfig,
	): SQL {

The PgInsertBase class provides conflict handling methods .onConflictDoNothing() and .onConflictDoUpdate().

	onConflictDoUpdate(
		config: PgInsertOnConflictDoUpdateConfig<this>,
	): PgInsertWithout<this, TDynamic, 'onConflictDoNothing' | 'onConflictDoUpdate'> {
		if (config.where && (config.targetWhere || config.setWhere)) {
			throw new Error(
				'You cannot use both "where" and "targetWhere"/"setWhere" at the same time - "where" is deprecated, use "targetWhere" or "setWhere" instead.',
			);
		}
		const whereSql = config.where ? sql` where ${config.where}` : undefined;
		const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : undefined;
		const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : undefined;
		const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
		let targetColumn = '';
		targetColumn = Array.isArray(config.target)
			? config.target.map((it) => this.dialect.escapeName(this.dialect.casing.getColumnCasing(it))).join(',')
			: this.dialect.escapeName(this.dialect.casing.getColumnCasing(config.target));
		this.config.onConflict = sql`(${
			sql.raw(targetColumn)
		})${targetWhereSql} do update set ${setSql}${whereSql}${setWhereSql}`;
		return this as any;
	}

PostgreSQL views and materialized views support execution refreshing via PgRefreshMaterializedView.

export class PgRefreshMaterializedView<TQueryResult extends PgQueryResultHKT>
	extends QueryPromise<PgQueryResultKind<TQueryResult, never>>
	implements RunnableQuery<PgQueryResultKind<TQueryResult, never>, 'pg'>, SQLWrapper
{
	static override readonly [entityKind]: string = 'PgRefreshMaterializedView';
 
	private config: {
		view: PgMaterializedView;
		concurrently?: boolean;
		withNoData?: boolean;
	};
 
	constructor(
		view: PgMaterializedView,
		private session: PgSession,
		private dialect: PgDialect,
	) {
		super();
		this.config = { view };
	}
 
	concurrently(): this {
		if (this.config.withNoData !== undefined) {
			throw new Error('Cannot use concurrently and withNoData together');
		}
		this.config.concurrently = true;
		return this;
	}
 
	withNoData(): this {
		if (this.config.concurrently !== undefined) {
			throw new Error('Cannot use concurrently and withNoData together');
		}
		this.config.withNoData = true;
		return this;
	}

Caution

Attempting to combine .concurrently() and .withNoData() on a materialized view refresh triggers an explicit error invariant check in both PostgreSQL and Gel implementations.

if (this.config.withNoData !== undefined) {
    throw new Error('Cannot use concurrently and withNoData together');
}

SingleStore Query Builders

SingleStore query building is directed by SingleStoreDialect and SingleStoreInsertBase. The dialect handles migration bootstrapping by verifying and creating the __drizzle_migrations table via session.execute(), and executes insert queries supporting ignore flags and duplicate key updates.

	buildInsertQuery({
		table,
		values,
		ignore,
		onConflict,
	}: SingleStoreInsertConfig): {
		sql: SQL;
		generatedIds: Record<string, unknown>[];
	} {

When executing inserts, SingleStore tracks primary key auto-increment columns to generate response IDs, formatting clauses with on duplicate key update.

	onDuplicateKeyUpdate(
		config: SingleStoreInsertOnDuplicateKeyUpdateConfig<this>,
	): SingleStoreInsertWithout<this, TDynamic, 'onDuplicateKeyUpdate'> {
		const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
		this.config.onConflict = sql`update ${setSql}`;
		return this as any;
	}

SQLite Query Builders

SQLite query compilation (SQLiteDialect) manages parameter formatting (?), string escaping (escapeString), and identifier quoting (escapeName).

	escapeName(name: string): string {
		return `"${name.replace(/"/g, '""')}"`;
	}
 
	escapeParam(_num: number): string {
		return '?';
	}
 
	escapeString(str: string): string {
		return `'${str.replace(/'/g, "''")}'`;
	}

The SQLiteInsertBase builder handles table insertion records, falling back to column defaults or null values when parameters are omitted, and supports .returning() and .onConflictDoNothing() / .onConflictDoUpdate() clauses.

	onConflictDoNothing(config: { target?: IndexColumn | IndexColumn[]; where?: SQL } = {}): this {
		if (!this.config.onConflict) this.config.onConflict = [];
 
		if (config.target === undefined) {
			this.config.onConflict.push(sql` on conflict do nothing`);
		} else {
			const targetSql = Array.isArray(config.target) ? sql`${config.target}` : sql`${[config.target]}`;
			const whereSql = config.where ? sql` where ${config.where}` : sql``;
			this.config.onConflict.push(sql` on conflict ${targetSql} do nothing${whereSql}`);
		}
		return this;
	}
Was this page helpful?