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

Can't create row with default values #685

Closed
codepunkt opened this issue Sep 8, 2023 · 3 comments
Closed

Can't create row with default values #685

codepunkt opened this issue Sep 8, 2023 · 3 comments
Labels
api Related to library's API enhancement New feature or request greenlit Ready for implementation postgres Related to PostgreSQL

Comments

@codepunkt
Copy link

It seems impossible to create a new row if a table has only auto-generated columns, e.g. postgres gen_random_uuid() id

model Cart {
  id    String     @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  items CartItem[] @relation

  @@map("carts")
}

I tried all variations of this I could think of, including using raw sql, but everything I tried resulted in SQL syntax errors:

db.insertInto('carts').columns([]).returningAll().executeTakeFirstOrThrow()

The query I need to create here is

insert into "carts" default values returning *

Is there a way to do this?

@koskimas
Copy link
Member

koskimas commented Sep 8, 2023

There's nothing built-in, but this should work https://kyse.link/?p=s&i=1kOLhQ0FgVdND0Dksk5a

@koskimas koskimas closed this as completed Sep 8, 2023
@koskimas koskimas reopened this Sep 8, 2023
@koskimas
Copy link
Member

koskimas commented Sep 8, 2023

Hmm.. Maybe it'd be worth it to add a defaultValues() method to the insert query builder.

@koskimas koskimas added enhancement New feature or request postgres Related to PostgreSQL api Related to library's API greenlit Ready for implementation labels Sep 8, 2023
@koskimas
Copy link
Member

koskimas commented Jan 2, 2024

Added in 0.27.1.

db.insertInto('person')
  .defaultValues()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Related to library's API enhancement New feature or request greenlit Ready for implementation postgres Related to PostgreSQL
Projects
None yet
Development

No branches or pull requests

2 participants