Skip to content

Commit

Permalink
[All] Fixed insert and update behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelelz committed Nov 19, 2023
1 parent 45ae32e commit 6c49e4f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
9 changes: 5 additions & 4 deletions drizzle-orm/src/mysql-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ')];
}
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions drizzle-orm/src/pg-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ')];
}
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions drizzle-orm/src/sqlite-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ')];
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6c49e4f

Please sign in to comment.