diff --git a/drizzle-orm/src/mysql-core/dialect.ts b/drizzle-orm/src/mysql-core/dialect.ts index cd01ef748..b74ae6b0a 100644 --- a/drizzle-orm/src/mysql-core/dialect.ts +++ b/drizzle-orm/src/mysql-core/dialect.ts @@ -99,9 +99,10 @@ export class MySqlDialect { const setSize = columnNames.length; return sql.join(columnNames.flatMap((colName, i) => { const col = tableColumns[colName]!; - const res = set[colName] - ? sql`${sql.identifier(col.name)} = ${set[colName]}` - : sql`${sql.identifier(col.name)} = ${col.onUpdateFn!()}`; + + const value = set[colName] ?? sql.param(col.onUpdateFn!(), col); + const res = sql`${sql.identifier(col.name)} = ${value}`; + if (i < setSize - 1) { return [res, sql.raw(', ')]; } @@ -416,7 +417,7 @@ export class MySqlDialect { const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col); valueList.push(defaultValue); // eslint-disable-next-line unicorn/no-negated-condition - } else if (col.onUpdateFn !== undefined) { + } else if (!col.default && col.onUpdateFn !== undefined) { const onUpdateFnResult = col.onUpdateFn(); const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col); valueList.push(newValue); diff --git a/drizzle-orm/src/pg-core/dialect.ts b/drizzle-orm/src/pg-core/dialect.ts index c332601c3..e11675cba 100644 --- a/drizzle-orm/src/pg-core/dialect.ts +++ b/drizzle-orm/src/pg-core/dialect.ts @@ -111,9 +111,10 @@ export class PgDialect { const setSize = columnNames.length; return sql.join(columnNames.flatMap((colName, i) => { const col = tableColumns[colName]!; - const res = set[colName] - ? sql`${sql.identifier(col.name)} = ${set[colName]}` - : sql`${sql.identifier(col.name)} = ${col.onUpdateFn!()}`; + + const value = set[colName] ?? sql.param(col.onUpdateFn!(), col); + const res = sql`${sql.identifier(col.name)} = ${value}`; + if (i < setSize - 1) { return [res, sql.raw(', ')]; } @@ -444,7 +445,7 @@ export class PgDialect { const defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col); valueList.push(defaultValue); // eslint-disable-next-line unicorn/no-negated-condition - } else if (col.onUpdateFn !== undefined) { + } else if (!col.default && col.onUpdateFn !== undefined) { const onUpdateFnResult = col.onUpdateFn(); const newValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col); valueList.push(newValue); diff --git a/drizzle-orm/src/sqlite-core/dialect.ts b/drizzle-orm/src/sqlite-core/dialect.ts index 1bf75789b..36944cfb2 100644 --- a/drizzle-orm/src/sqlite-core/dialect.ts +++ b/drizzle-orm/src/sqlite-core/dialect.ts @@ -69,9 +69,10 @@ export abstract class SQLiteDialect { const setSize = columnNames.length; return sql.join(columnNames.flatMap((colName, i) => { const col = tableColumns[colName]!; - const res = set[colName] - ? sql`${sql.identifier(col.name)} = ${set[colName]}` - : sql`${sql.identifier(col.name)} = ${col.onUpdateFn!()}`; + + const value = set[colName] ?? sql.param(col.onUpdateFn!(), col); + const res = sql`${sql.identifier(col.name)} = ${value}`; + if (i < setSize - 1) { return [res, sql.raw(', ')]; } @@ -384,7 +385,7 @@ export abstract class SQLiteDialect { const defaultFnResult = col.defaultFn(); defaultValue = is(defaultFnResult, SQL) ? defaultFnResult : sql.param(defaultFnResult, col); // eslint-disable-next-line unicorn/no-negated-condition - } else if (col.onUpdateFn !== undefined) { + } else if (!col.default && col.onUpdateFn !== undefined) { const onUpdateFnResult = col.onUpdateFn(); defaultValue = is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col); } else {