-
Notifications
You must be signed in to change notification settings - Fork 282
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
Batch inserts use "undefined" instead of falling back on default value #1310
Comments
Can't reproduce this. You can see here that |
By the way, you should never have optional fields in your table interface. Mark nullable columns as nullable. Also mark columns generated by the database using interface TestTable {
id: number
created_at: Generated<Date> // generated by DB --> use Generated
optional_field: string | null // nullable, NOT optional
} Optionality is automatically determined by Kysely. You should also never use the type Test = Selectable<TestTable>
// { id: number, created_at: Date, optional_field: string | null }
type NewTest = Insertable<TestTable>
// { id: number, created_at?: Date, optional_field?: string | null }
type TestUpdate = Updateable<TestTable>
// { id?: number, created_at?: Date, optional_field?: string | null } |
Hi, I agree that I can't replicate in the playground. This is a better example that matches what I have, but the generated SQL when I run it locally uses "undefined" instead of "DEFAULT". This makes me wonder if there's a missing middleware or a version mismatch or something like that. I am using Postgres 15.4 and Kysley 0.27.4. Could you help me understand what the playground uses? Furthermore, if I explicitly filter out "undefined" key/value pairs, then it works locally (I get DEFAULT instead of undefined):
PostgreSQL 15.4 (Debian 15.4-1.pgdg120+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit |
Hey 👋 Can you provide a reproduction repository? |
Sure, do you have a starter repo I can fork? Here's another interesting thing I'm noticing. I tried to write a test, and I can't get it to to fail. It looks correct just like with the playground. Both the
Whereas my actual app uses
|
To summarize: The Kysley playground and my own test environment both work as expected. They both use:
However, in my app, I have to either:
My app uses kysley 0.27.4, Postgres 15.4, and:
I have logged the generated SQL for both chunk sizes, and when it works (chunkSize = 1) I see "DEFAULT", but when it doesn't work (chunkSize > 1) I see "undefined". It's the same exact code besides the chunk size. |
@nrathi Sure, do you have a starter repo I can fork? We don't.
|
I also can't replicate this any other way. Our test suite has tests for this case. kysely/test/node/src/insert.test.ts Line 689 in 67e413e
|
Finally got the repro!!! |
So it seems like if the insert contains any OTHER column that's nullable without a default, that's when the bug hits. This can be "number | undefined" or "number | null", same bug:
|
Actually, looks like any unrelated undefined value will do it. Another repro: https://kyse.link/0feir |
It's probably a bug in the values argument parser - plain JavaScript edge case not being covered. |
Hi @nrathi, |
@naorpeled I wasn't taking a crack at all, but perhaps @igalklebanov has already made progress? |
I haven't touched this one. |
Thanks for confirming @igalklebanov and thanks for offering to help @naorpeled! |
@nrathi you want this one? |
I do not; I lost a few days to debugging already so I was planning on unblocking myself with:
Now that I comfortably understand the core issue. @naorpeled I appreciate you jumping in and offering to take a look! |
When doing batch inserts (multiple values in a single INSERT), missing values are being replaced with DEFAULT values, but undefined values are not. Single inserts handle both missing and undefined values correctly.
Edit: The original repro wasn't great; the real repro is here: https://kyse.link/0feir
The text was updated successfully, but these errors were encountered: