Many to many - Planetscale
Anyone here has an example of declaring many to many relationship when using Planetscale?
https://orm.drizzle.team/docs/rqb#many-to-many
When using Planetscale this section doesn't work at all
Relational queries ā DrizzleORM
Drizzle ORM | %s
19 Replies
Do you still need help ?
I think I could actually use help here. I'd rather avoid doing things manually via a bunch of table joins, if I can avoid it.
What doesn't work in the many to many example in the docs?
The important part for planetscale is to not use foreign keys. The example in the docs is not using foreign keys
@Angelelz With this using
relations
Can you add additional columns when using relations()?I don't understand the question
You can have as many relations as you want
Say i have a Workout with multiple activities, but i want to define an order on the relation
so an activity is always in the same spot on the workout.
My first thought was to add the column
order
on the "junction table" but maybe it should be on the activity table itself?
And thought would it be possible to define on the relations
okay disregard i think i got it, was confused move between the docs
You need both the junction table and the relations, i just was looking at the highlighted portion in the docI would put the order column in the junction table
Because the order will be unique per activity <-> workout
I have a junction table and the relations setup, like the example, but in the junction table,
usersToGroups
, is referencing foreign keys. I can give my specific example in a couple hours. Iām AFK right now.
Here's an example from a schema I wrote out that has a many-to-many relationship of books and authors:
When I attempt to push that schema I get this message from PlanetScale:
Presumably that's because of the references(() => books.id)
and references(() => authors.id)
usage.
When I remove that, like it says you can do in the docs, I'm uncertain how I can write my query. Doing this results in an error (using tRPC) when attempting to launch Drizzle Studio:
This is correct
Your problem is an error in your relation definition
For a Many to Many, you need another table in the middle:
Author <-> AuthorsToBooks <-> Books
Your booksRelations is referencing the authors directly:
Same as the authorsRelations:
You need to make booksRelations reference authorsToBooks
and authorsRelations reference AuthorstoBooks
Gotcha! I see where I went wrong. The relation on each is to the join table.
Drizzle was not able to infer the relation due to this error
Oh my gosh. I knew it was something idiotic and I just overlooked it in the docs. š¤¦āāļø
Thank you so much, @Angelelz !
I'm not the OP, so I can't close this, but I'm golden now. Thanks for all the help everyone! Sorry I didn't look at the highlights in the docs hard enough.
On this topic, I have my two tables created, along with a junction table with the ids. I have successfully used the
I got this far using drizzle docs: But because its coming from the junction table, the data is no longer in a useful format, and I am a bit lost. I was hoping to stick to the SQL style as much as possible so I better learn it, so any guidance would be appreciated here!
db.query()
syntax so know the tables work, but can't figure out how to get data using the db.select()
syntax, and can't seem to find any examples that include how to actually use the response.I got this far using drizzle docs: But because its coming from the junction table, the data is no longer in a useful format, and I am a bit lost. I was hoping to stick to the SQL style as much as possible so I better learn it, so any guidance would be appreciated here!
I suggest you use the RQB, print the query to the console so you can try to replicate it using the crud API
RQB? Sorry I don't think I know what that means
Relational query builders https://orm.drizzle.team/docs/rqb
Drizzle Queries - DrizzleORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind
ahhh. So my attempt to learn how to do it this way is more trouble than worth then? š
I was starting to lean this way
thanks
No, I mean, you are getting the data you need with that query
It's just not formatted in a nice way
You could aggregate it with JS/TS if you wanted to
sorry, must have misread. I think I just assumed this was a frequent enough use case, it would already be a "solved problem" without needing to manually restructure all the data.
for anyone curious, I was able to achieve closer to what I wanted using the magic sql function and GROUP_CONCAT: