Hong
Hong
DTDrizzle Team
Created by Hong on 1/14/2024 in #help
Question about relations
I am trying to migrate from Prisma to Drizzle. But after reading the docs, I can't understand it. How to add relations to connect them? One user should have many comments. Can someone give me a real-world case so that I can learn faster?
export const users = mysqlTable('user', {
id: varchar('id', { length: 255 }).notNull().primaryKey(),
name: varchar('name', { length: 255 }),
email: varchar('email', { length: 255 }).notNull(),
emailVerified: timestamp('emailVerified', {
mode: 'date',
fsp: 3
}).defaultNow(),
image: varchar('image', { length: 255 })
})
export const users = mysqlTable('user', {
id: varchar('id', { length: 255 }).notNull().primaryKey(),
name: varchar('name', { length: 255 }),
email: varchar('email', { length: 255 }).notNull(),
emailVerified: timestamp('emailVerified', {
mode: 'date',
fsp: 3
}).defaultNow(),
image: varchar('image', { length: 255 })
})
export const comments = mysqlTable('comment', {
id: varchar('id', { length: 255 }).notNull().primaryKey(),
body: varchar('body', { length: 255 }).notNull(),
created_at: timestamp('created_at', {
mode: 'date',
fsp: 6
}).defaultNow(),
updated_at: timestamp('updated_at', {
mode: 'date',
fsp: 6
}).defaultNow(),
userId: varchar('userId', { length: 255 })
.notNull()
.references(() => users.id)
})
export const comments = mysqlTable('comment', {
id: varchar('id', { length: 255 }).notNull().primaryKey(),
body: varchar('body', { length: 255 }).notNull(),
created_at: timestamp('created_at', {
mode: 'date',
fsp: 6
}).defaultNow(),
updated_at: timestamp('updated_at', {
mode: 'date',
fsp: 6
}).defaultNow(),
userId: varchar('userId', { length: 255 })
.notNull()
.references(() => users.id)
})
Also, I am using NextAuth. Do I need to add relations to connect users with account and session?
3 replies