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

Improve explicit many-to-many #59

Closed
John0x opened this issue May 9, 2022 · 4 comments
Closed

Improve explicit many-to-many #59

John0x opened this issue May 9, 2022 · 4 comments

Comments

@John0x
Copy link
Contributor

John0x commented May 9, 2022

It would be nice if you could use explicit m2m relations the same way as implicit ones.
Currently you have to provide extra information for explicit relations, but I think you could probably infer that stuff from the prisma schema?

@Brendonovich
Copy link
Owner

What do you mean by 'have to provide extra information'? I'm genuinely confused 😅

@John0x
Copy link
Contributor Author

John0x commented May 9, 2022

Example

Schema

model Program {
  id           Int      @id @default(autoincrement()) 
  grantor GrantorOnPrograms[]
}

model Grantor {
  name     String              @id
  programs GrantorOnPrograms[]
}

model GrantorOnPrograms {
  id          Int     @id @default(autoincrement())
  program     Program @relation(fields: [programId], references: [id])
  programId   Int
  grantor     Grantor @relation(fields: [grantorName], references: [name])
  grantorName String
} 

Insert

You can't just do:

  db.program()
      .create(
          program::id::set(123),
          vec![program::grantor::link(vec![
              grantor::name::equals("whatever"),
          ])],
      )
      .exec()
      .await?;

You have to use the join table:

    db.program()
        .create(
            program::origin::set(construct_program_link(url)),
            vec![program::grantor::link(vec![
                grantor_on_programs::id::equals(12345),
            ])],
        )
        .exec()
        .await?;

It would be nice if the client automatically resolves the join table

References

https://www.prisma.io/docs/concepts/components/prisma-schema/relations/many-to-many-relations#explicit-many-to-many-relations

@Brendonovich
Copy link
Owner

The problem with doing so is that the client isn't aware of what a join table is and I don't plan for it to ever be. That is not a feature Prisma Client JS has, and it kind of defeats the purpose of an explicit join table in the first place, since they are supposed to yield control of the join table from Prisma to you.

If you want nested creates, which would allow the Program and GrantorOnPrograms records to be created in the same query, I have planned it in #44 but probably won't implement it for a while.

Also, if you have a join table like the example you provided, it's probably worth just using an implicit one. Not sure of your exact use case though.

@John0x
Copy link
Contributor Author

John0x commented May 10, 2022

The problem with doing so is that the client isn't aware of what a join table is and I don't plan for it to ever be. That is not a feature Prisma Client JS has, and it kind of defeats the purpose of an explicit join table in the first place, since they are supposed to yield control of the join table from Prisma to you.

If you want nested creates, which would allow the Program and GrantorOnPrograms records to be created in the same query, I have planned it in #44 but probably won't implement it for a while.

Also, if you have a join table like the example you provided, it's probably worth just using an implicit one. Not sure of your exact use case though.

Ah, okay. Understandable. I thought that the js client would allow the join tables to be automated.
Yep, I would have preferred implicit joins, but we have some unfortunate restrictions on the db schema 😬

@John0x John0x closed this as completed May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants