Relation many record MySQL
How to create many record using forEach in array and relrelation it to parent table in MySQL like Prisma
11 Replies
Hello, @Kai Revona! As for now, there is no
create
method in relational queries, so you have to use core queries such as insert().values()
for posts and hashtags with your logicPlease give me an example to help me understand better
Prisma spoiled devs too much IMO. The usual way to do this, is starting a transaction, insert the post and receive the post id, then insert the tags with the post id.
It could look something like this:
This is technically correct. Although I would change the for loop for an upsert type of insert with
onConflictDoUpdate()
or a promise.allSettled()
to avoid the blocking behavior you'll have here sending one tag after the otherye, super, thanks
Can you update your code with onConflictDoUpdate() or promise.allSettled()
Please help me because I am junior
@Kai Revona
Thank you
Schema
And relations
Function
hashtagId return string and cannot able to create hashtagId. I have to use insertId because MySQL doesn't support returning. Please help me to fix this error
@Kai Revona if you don't use relational queries you don't need relation tables. Then you have to update your schemas code.
schema.ts
your function
Here there are docs, where you can find useful information related to this code:
1. https://orm.drizzle.team/docs/column-types/mysql#serial
2. https://orm.drizzle.team/docs/insert#on-duplicate-key-update
3. https://orm.drizzle.team/docs/joins
Drizzle ORM - next gen TypeScript ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Drizzle ORM - next gen TypeScript ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Drizzle ORM - next gen TypeScript ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Ok, i will test them soon and reply you