How do insert a one-to-one record in an elegant way?
I know how to query a row with a one-to-one relationship which is mentioned in the doc of drizzle, but how to insert it?
5 Replies
btw, prisma gonna be this: prisma.user.create({name: "bob", profile: create: {age: 20}})
Prisma is currently a higher abstraction than Drizzle
In drizzle you make the database calls and transactions yourself
Which is the way you do it in plain SQL
In this case you need to start a transaction, insert your user and get back the id, then insert your profile with the userId foreign key if needed
ok, got it, thanks for your information! but I think is it necessary to put this into the doc maybe?
according to your reply, I came out with another question that is not that important: "Insert my user and get ID" but Mysql doesn't support returning(), so how to get the ID and use it as a reference for another table?
I think that the docs will be rewritten and made more beginer friendly
If your ID is an autoincrement, mysql will return the inserted ID in one of the keys of the response object
If not, you'll need to either create your id outside sql and send it that way, or select it from the db afterwards
Thank you so much for your detailed response!