Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve edge case with varying shapes in multiple item inserts causing undefined in parameters. #1311

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading