Does drizzle support `disconnect` relations ?

Does drizzle support disconnect feature on relations during update query similar to prisma ?
6 Replies
Angelelz
Angelelz•13mo ago
Could you elaborate for those of us not familiar with the prisma feature?
ReactPoriyaalar
ReactPoriyaalarOP•13mo ago
In prisma to remove relations between two sides you can use disconnect which is part of the client . It can be used for one to many relationships https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#disconnect-a-related-record
Prisma
Relation queries (Concepts)
Prisma Client provides convenient queries for working with relations, such as a fluent API, nested writes (transactions), nested reads and relation filters.
itsyoboieltr
itsyoboieltr•13mo ago
and also connect 😄 I also wonder how it is possible to work with relations in drizzle
Angelelz
Angelelz•13mo ago
Drizzle doesn't extract you away so much from SQL. Connect and disconnect is an abstraction they invented In drizzle, you can effectively achieve this by setting the foreign key to null Let's say you have a users table and a posts table defined like this:
const users = mysqlTable('users', {
id: int('id').primaryKey(),
name: text('name')
})
const posts = mysqlTable('posts', {
id: int('id').primaryKey(),
title: text('title').notNull(),
content: text('content').notNull(),
authorId: int('author_id').references(() => users.id)
})
const users = mysqlTable('users', {
id: int('id').primaryKey(),
name: text('name')
})
const posts = mysqlTable('posts', {
id: int('id').primaryKey(),
title: text('title').notNull(),
content: text('content').notNull(),
authorId: int('author_id').references(() => users.id)
})
The relations are like this:
const userRelations = relation(users, ({many}) => ({
posts: many(posts)
})
const postsRelations = relation(posts, ({one}) => ({
author: one(users, {
fields: [posts.authorId],
references: [users.id]
})
})
const userRelations = relation(users, ({many}) => ({
posts: many(posts)
})
const postsRelations = relation(posts, ({one}) => ({
author: one(users, {
fields: [posts.authorId],
references: [users.id]
})
})
You can "disconnect" a post from an user by doing:
await db.update(posts).set({ authorId: null }).where(eq(posts.id, 1))
await db.update(posts).set({ authorId: null }).where(eq(posts.id, 1))
Again, connect and disconnect is an abstraction invented by prisma. But its basically the foreign key that connects the models
itsyoboieltr
itsyoboieltr•13mo ago
I think everyone got too comfortable using prisma lol, but this actually makes sense, atleast we learn some stuff behind the scenes :DD
ReactPoriyaalar
ReactPoriyaalarOP•13mo ago
Thanks for clarifying this. I actually found this out with the basic knowledge of SQL . Also now may be in retrospect I feel disconnect and connect are not so much needed abstractions
Want results from more Discord servers?
Add your server