Update relationship within 1 query

is it possible to update relationships? for example i have a one to one relationship between users and notifications but i couldn't find any example of how i could update the notifications of a user in 1 query
const updateduser = await db
.update(users)
.set({ email: userAttributes.email }) //i would expect `notifications` to be an option in `set`
.where(eq(users.id, userAttributes.id))
.returning();
const updateduser = await db
.update(users)
.set({ email: userAttributes.email }) //i would expect `notifications` to be an option in `set`
.where(eq(users.id, userAttributes.id))
.returning();
same question goes for many to many relationships lets say i have a many to many between users and interests i want to update the user attributes and the interests of the user do i have to: 1. update user attributes 2. delete the current interests of the user from the association table 3. insert updated interests of the user
2 Replies
Angelelz
Angelelz16mo ago
You can't update several tables at the same time in SQL Remember users and notifications are different tables. Same for the many to many, those are different tables, and have to be updated in separate queries The correct SQL/Drizzle way to do this is within a transaction
Angelelz
Angelelz16mo ago
You can read about how to do it in drizzle here: https://orm.drizzle.team/docs/transactions
Want results from more Discord servers?
Add your server