两个棱镜模型之间的一对多和多对多关系
One-to-many and may-to-many relation between two prisma model
我正在做一个副项目,遇到了一个我不确定如何解决的问题。我创建了两个模型,一个用于用户,一个用于项目。它们之间的关系是多对多的,因为一个用户可以有多个项目,但我还想在项目模型中添加一个createdBy,这应该是一对一的,因为每个项目只能有一个用户谁创造了它。这是我的模型的样子:
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
accounts Account[]
sessions Session[]
projects Project[]
Project Project[] @relation("userId")
}
model Project {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
name String
favourite Boolean @default(false)
archived Boolean @default(false)
users User[]
about String?
archivedAt String?
deletedAt String?
createdBy User @relation("userId", fields: [userId], references: [id])
userId String
}
从 prisma migrate dev 得到的错误是
Step 1 Added the required column userId
to the Project
table without a default value. There are 2 rows in this table, it is not possible to execute this step.
不确定我做错了什么,因为我几乎是一个数据库建模新手。任何帮助将不胜感激。
要使用 One-To-Many
实现 Many-to-Many
,您需要将 schema.prisma
文件设置为:
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
projects Project[]
userProjects UserProjects[]
}
model Project {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
name String
favourite Boolean @default(false)
archived Boolean @default(false)
about String?
archivedAt String?
deletedAt String?
createdBy User @relation(fields: [user_id], references: [id])
user_id String
projectUsers UserProjects[]
}
model UserProjects {
project Project @relation(fields: [project_id], references: [id])
project_id Int
user User @relation(fields: [user_id], references: [id])
user_id String
@@id([project_id, user_id])
}
应该迁移成功。
我正在做一个副项目,遇到了一个我不确定如何解决的问题。我创建了两个模型,一个用于用户,一个用于项目。它们之间的关系是多对多的,因为一个用户可以有多个项目,但我还想在项目模型中添加一个createdBy,这应该是一对一的,因为每个项目只能有一个用户谁创造了它。这是我的模型的样子:
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
accounts Account[]
sessions Session[]
projects Project[]
Project Project[] @relation("userId")
}
model Project {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
name String
favourite Boolean @default(false)
archived Boolean @default(false)
users User[]
about String?
archivedAt String?
deletedAt String?
createdBy User @relation("userId", fields: [userId], references: [id])
userId String
}
从 prisma migrate dev 得到的错误是
Step 1 Added the required column
userId
to theProject
table without a default value. There are 2 rows in this table, it is not possible to execute this step.
不确定我做错了什么,因为我几乎是一个数据库建模新手。任何帮助将不胜感激。
要使用 One-To-Many
实现 Many-to-Many
,您需要将 schema.prisma
文件设置为:
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
projects Project[]
userProjects UserProjects[]
}
model Project {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
name String
favourite Boolean @default(false)
archived Boolean @default(false)
about String?
archivedAt String?
deletedAt String?
createdBy User @relation(fields: [user_id], references: [id])
user_id String
projectUsers UserProjects[]
}
model UserProjects {
project Project @relation(fields: [project_id], references: [id])
project_id Int
user User @relation(fields: [user_id], references: [id])
user_id String
@@id([project_id, user_id])
}
应该迁移成功。