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

[BUG]: Insert empty array should be valid #1719

Closed
jakeleventhal opened this issue Dec 28, 2023 · 4 comments
Closed

[BUG]: Insert empty array should be valid #1719

jakeleventhal opened this issue Dec 28, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@jakeleventhal
Copy link

What version of drizzle-orm are you using?

0.29.2

What version of drizzle-kit are you using?

0.20.9

Describe the Bug

throw new Error('values() must be called with at least one value');

Why would this not just be a no-op?

Expected behavior

If the values parameter is an object, I would expect a required value but if it is an array, I think length 0 is acceptable input.

Environment & setup

No response

@jakeleventhal jakeleventhal added the bug Something isn't working label Dec 28, 2023
@Angelelz
Copy link
Collaborator

I don't think you should just delete this type of safeguards and not include tests verifying it didn't break another area of the codebase.
BTW, If you want to do something similar to what it seems like you want, the tested way is:

await db.insert(users).values({});

See for example this test. And the test right after that one.

The problem with accepting an empty array, is that it disables all the type guards around the inserted value.
Say you have a table with not null values without default like this:

const myTable = pgTable("my_table", {
  id: serial('id').primaryKey(),
  name: text('name').notNull()
}
// if you attempt to insert the way you describe like this:
const insertedValue: typeof myTable.$inferInsert[] = []; // an empty array is a valid array for anything.

// This next line is going to fail at the database level 
const response = await db.insert(myTable).values(insertedValue);

Instead, you could use an empty object, to signify that you want to insert one row to the DB and the types will check it.

@jakeleventhal
Copy link
Author

What would you say then to the idea of having some special exception raised (to skip all the logic of even contacting the db) and just return [] typed as the proper type but with length of 0

@Angelelz
Copy link
Collaborator

I don't think this would be actionable until #376 is closed. And to be honest with you, I personally don't think it's a good idea. I don't really know why us dev just don't want to deal with errors.
IMO, drizzle definitely needs to tell the user that an error has occurred, and returning an empty array is not the way to do that properly.

@jakeleventhal
Copy link
Author

I don't disagree. I will close this and the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants