Skip to content

Commit

Permalink
fix: resolve edge case with varying shapes in multiple item inserts c…
Browse files Browse the repository at this point in the history
…ausing `undefined` in parameters. (#1311)

Co-authored-by: Naor Peled <[email protected]>
Co-authored-by: Igal Klebanov <[email protected]>
  • Loading branch information
3 people authored Jan 18, 2025
1 parent fb676c1 commit 660dfb4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/parser/insert-values-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@ function parseRowValues(
})

let hasUndefinedOrComplexColumns = false
let indexedRowColumns = rowColumns.length

for (const col of rowColumns) {
const columnIdx = columns.get(col)

if (isUndefined(columnIdx)) {
indexedRowColumns--
continue
}

Expand All @@ -117,7 +119,7 @@ function parseRowValues(
rowValues[columnIdx] = value
}

const hasMissingColumns = rowColumns.length < columns.size
const hasMissingColumns = indexedRowColumns < columns.size

if (hasMissingColumns || hasUndefinedOrComplexColumns) {
const defaultValue = DefaultInsertValueNode.create()
Expand Down
36 changes: 36 additions & 0 deletions test/node/src/insert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,42 @@ for (const dialect of DIALECTS) {
await query.execute()
})

// https://github.com/kysely-org/kysely/issues/1310
it('should insert multiple rows while falling back to default values in partial rows - different shapes edge case issue - issue #1310', async () => {
const query = ctx.db.insertInto('person').values([
{
gender: 'female',
marital_status: 'divorced', // <--- only exists here.
children: undefined, // <--- always undefined explicitly.
},
{
gender: 'female',
children: undefined, // <--- always undefined explicitly.
},
])

testSql(query, dialect, {
postgres: {
sql: `insert into "person" ("gender", "marital_status") values ($1, $2), ($3, default)`,
parameters: ['female', 'divorced', 'female'],
},
mysql: {
sql: `insert into \`person\` (\`gender\`, \`marital_status\`) values (?, ?), (?, default)`,
parameters: ['female', 'divorced', 'female'],
},
mssql: {
sql: `insert into "person" ("gender", "marital_status") values (@1, @2), (@3, default)`,
parameters: ['female', 'divorced', 'female'],
},
sqlite: {
sql: `insert into "person" ("gender", "marital_status") values (?, ?), (?, null)`,
parameters: ['female', 'divorced', 'female'],
},
})

await query.execute()
})

it('should insert multiple rows while falling back to default values in partial rows - undefined/missing columns', async () => {
const query = ctx.db.insertInto('person').values([
{
Expand Down

0 comments on commit 660dfb4

Please sign in to comment.