Home / Function/ onConflictDoUpdate() — drizzle-orm Function Reference

onConflictDoUpdate() — drizzle-orm Function Reference

Architecture documentation for the onConflictDoUpdate() function in insert.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  cb84ca6f_f73b_34f7_b84d_8d8d46727562["onConflictDoUpdate()"]
  8e4153bf_b365_8b49_3724_c65caf9729e7["PgInsertBase"]
  cb84ca6f_f73b_34f7_b84d_8d8d46727562 -->|defined in| 8e4153bf_b365_8b49_3724_c65caf9729e7
  style cb84ca6f_f73b_34f7_b84d_8d8d46727562 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

drizzle-orm/src/pg-core/query-builders/insert.ts lines 369–389

	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;
	}

Domain

Subdomains

Frequently Asked Questions

What does onConflictDoUpdate() do?
onConflictDoUpdate() is a function in the drizzle-orm codebase, defined in drizzle-orm/src/pg-core/query-builders/insert.ts.
Where is onConflictDoUpdate() defined?
onConflictDoUpdate() is defined in drizzle-orm/src/pg-core/query-builders/insert.ts at line 369.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free