Insert One to Many
How can I insert relations as well at create the parent?
My schema:
I want to add products to the insert for creating the new journey. Currently using a transaction but not sure what the code needs to be for linking products to the journey?
31 Replies
hum I don't use relation api (I was there before this api lol)
let see what we can do
you want to create products and then link them to a journey?
Yes thats right. Or even better so, simply query these products and then link them to the journey. (As they may have already been created)
just checking what kind of tape
relations
is creating
to you have the generated sql ?I've only got those two tables. Lemme link it to ya
many to many relation so a product can be on 0 or more journey, and journey can have 0 or many products, right?
One to many i think. Journeys will have products but products wont have reference to journeys
ah that's true that there is no foreign key concept in mysql/planetscale
(postgres user here hehe)
The query part is simple just the insert. For example: this is what the query could look like:
But yeah all the docs just bloody reference query queries (hehe) and not any insert/updates with relations
I think there is a missing pieces\
Relational queries – DrizzleORM
Drizzle ORM | %s
usersRelations points to a 3rd table joining user and group
this is what troubled me, I didn't see where this relation lived on the db
or I miss something related to planetscale?
Yeah that isn't there atm.
Only those 2 tables
I'd always expect a 3rd like journeyProduct table for many to many
I wonder how does the one to many magic work then without foreign keys
in prisma world, it will create this, but with drizzle it's on your own
col association I guess
like a weak foreign key
So if i go with something like from that example:
What will my insert query look like ? For my Journey/Product scenario
I'm guessing something like
?
ye
So my query would look likeeee
this maybe?
Depending on what you want in your POST.
Inserting new products on new journey.
Inserting products in new journey
Updating journey with new products, or existing products
Since relation query seems only for reading, you'll insert the classic way
Like that?
you can or you could just let the db giving you created id like:
Yeah I was thinking of that but mysql apparently does let you use returning
Or am i wrong
oh sorry
If im not then postgres might be the way xD
you are right, I misread >_>
So to round this all up i guess, will drizzle know to pull in the correct products if I use that query from earlier?
Or is there more config to define: it's now looking like this all together:
I understand it like you wrote
We shall see I guess, if that doesn't work its for another ticket i guess 😛 Thank you for your help bro
You are welcome. Again sorry, I'm more a Postgres guy 😄
no returning trouble me so much lol It makes things harder for me :/
You and me both, god knows why Im using mysql