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?
2 Replies
Hong
HongOP11mo ago
Prisma:
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime? @map("email_verified")
image String? @db.Text
accounts Account[]
sessions Session[]
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
Comment Comment[]
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime? @map("email_verified")
image String? @db.Text
accounts Account[]
sessions Session[]
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
Comment Comment[]
}
model Comment {
id String @id @default(cuid())
body String
user User @relation(fields: [userId], references: [id])
userId String
created_at DateTime @default(now())
updated_at DateTime @updatedAt
Post Post @relation(fields: [postId], references: [id])
postId String
children Comment[] @relation("comment_children")
parent Comment? @relation("comment_children", fields: [parentId], references: [id], onDelete: NoAction, onUpdate: NoAction)
parentId String?

@@index([userId])
@@index([parentId])
@@index([postId])
}
model Comment {
id String @id @default(cuid())
body String
user User @relation(fields: [userId], references: [id])
userId String
created_at DateTime @default(now())
updated_at DateTime @updatedAt
Post Post @relation(fields: [postId], references: [id])
postId String
children Comment[] @relation("comment_children")
parent Comment? @relation("comment_children", fields: [parentId], references: [id], onDelete: NoAction, onUpdate: NoAction)
parentId String?

@@index([userId])
@@index([parentId])
@@index([postId])
}
Angelelz
Angelelz11mo ago
Drizzle ORM - next gen TypeScript ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Want results from more Discord servers?
Add your server