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?] [Documentation] Implicit n:m in prisma does not have @@id (*explicit* does) #9287

Closed
1 task done
Philzen opened this issue Oct 10, 2023 · 2 comments
Closed
1 task done
Labels
bug/needs-info More information is needed for reproduction

Comments

@Philzen
Copy link
Contributor

Philzen commented Oct 10, 2023

What's not working?

I was trying to understand how to create SDL for a composite primary key (but failed so far so asked over here) which led me to https://redwoodjs.com/docs/schema-relations#many-to-many-relationships which mentions the @@id token. There it says:

Prisma's implicit many-to-many relationships create a table without a single field marked with the @id attribute. Instead, it uses a similar attribute: @@id to define a multi-field ID.

Looking at the prisma docs for implicit vs. explicit n:m relations, it shows clearly that only explicit have an @@id primary key.

The Redwood docs go on to say:

Since there's no single @id field in implicit many-to-many relationships, you can't use the SDL generator with the --crud flag.

That is true, there is no @id field in implicit (but there is, however, a composite primary key in explicit) relationships.

So what is unclear about reading this section (and browsing the page) is:

  1. isn't that first doc section actually referring to explicit n:m relationships?
  2. so what about primary composite keys (@@id) at all?
  • I'm interested in working on this (but i have virtually no time atm)
@Philzen Philzen added the bug/needs-info More information is needed for reproduction label Oct 10, 2023
@Tobbe
Copy link
Member

Tobbe commented Nov 28, 2023

Sorry this issue slipped through without any response @Philzen.

Prisma's implicit many-to-many relationships create a table without a single field marked with the @id attribute. Instead, it uses a similar attribute: @@id to define a multi-field ID.

I still think this is correct. Notice how it says Prisma creates a table. The table isn't visible in your schema file. So you won't see the @@id stuff. If you inspect your DB though, I'm sure you'll find this table that prisma created for you, and it should have constraints/keys matching up with what's essentially @@id. Exactly how this looks will depend on what DB engine you use.

And to answer your second question, the answer is unfortunately that RW's generators don't support composite IDs. You need to create a separate id field and then use @@unique to get the same effect as @@id. I know that can be a little annoying, but shouldn't actually matter in practice.

For now we don't plan on adding support for this to our generators. Or other more advanced features for that matter. The generator code is plenty complex as it is already 😅

That said - if someone from the community came with a clean PR for adding this we'd be very grateful 😉

Closing this for now. For anyone looking to pick this up, the next step would be to submit a well tested PR 🙏

@Tobbe Tobbe closed this as not planned Won't fix, can't repro, duplicate, stale Nov 28, 2023
@Tobbe
Copy link
Member

Tobbe commented Nov 28, 2023

Ohh, I should add - any doc updates to make the first point about @@id and implicit vs explicit relations more clear are of course also welcome 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/needs-info More information is needed for reproduction
Projects
None yet
Development

No branches or pull requests

2 participants