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

Allow LinkOptional - style FK constraints #1142

Closed
nettrino opened this issue Jan 1, 2024 · 5 comments
Closed

Allow LinkOptional - style FK constraints #1142

nettrino opened this issue Jan 1, 2024 · 5 comments
Labels
kind/feature A request for a new feature.

Comments

@nettrino
Copy link

nettrino commented Jan 1, 2024

It is possible to have a FK constraints that are potentially empty in prisma, similar to the following

model Bar {
    id          String    @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
}

model Foo {
    id    String    @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
    barId String?   @map("bar_id") @db.Uuid
    bar   Bar? @relation(fields: [bar_id], references: [id])
}

When emitting Create and Upsert directives, Link operations where an empty string is passed will fail in the attempt to convert to a UUID. As a result, one needs to be creating more complex if/else conditions to account for possibly empty foreign keys. It would be useful to either expose a LinkOptional directive or equivalent to allow users to create a single Create or Upsert statement

@steebchen
Copy link
Owner

steebchen commented Jan 2, 2024

The following should work for single keys:

var v *string
created, err := client.Post.FindUnique(
    Post.ID.Equals("a"),
).Update(
    Post.Author.Link(
        User.ID.EqualsIfPresent(v),
    ),
).Exec(ctx)
if err != nil {
    t.Fatalf("fail %s", err)
}

Probably a LinkOptional would be better though; and it would also work with composite keys

@steebchen steebchen added the kind/feature A request for a new feature. label Jan 2, 2024
@nettrino
Copy link
Author

nettrino commented Jan 2, 2024

This is fine when the record exists already, however it forces a secondary lookup once you want to create a record and optionally link it to multiple tables depending on whether a FK is present, unless I'm misunderstanding the API which is highly likely

@steebchen
Copy link
Owner

Hm can you maybe provide an example code of how you would do it right now and how you would wish to do it? So I can fully understand your use-case.

@nettrino
Copy link
Author

nettrino commented Jan 4, 2024

As discussed offline on discord, it is possible do a CreateOne operation directly and use something like

report, err = client.Report.CreateOne(
  db.Report.Workspace.Link(
    db.Workspace.ID.Equals(workspaceId),
  ),
  
  db.Report.Repository.Link(
    db.Repository.ID.EqualsIfPresent(repositoryId),
  ),
...

which addresses the constraint

@nettrino nettrino closed this as completed Jan 4, 2024
@github-project-automation github-project-automation bot moved this from Triage to Done in Prisma Client Go Public Jan 4, 2024
@steebchen
Copy link
Owner

Indeed. I have also created #1146 and #1145 as minor tasks as a follow up on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature.
Projects
Development

No branches or pull requests

2 participants