你好,我正在尝试实现一个create语句,它接受另一个模型的数组。下面是我的模式
model House {
id String @id
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
property_name String @db.VarChar(30)
house_type String @db.VarChar(10)
beds Int
baths Int
workspace Int
occupied Boolean @default(false)
stay DateTime[]
amenities Amenity[]
room Room[]
zipcode String @db.VarChar(5)
address Address @relation(fields: [address_id], references: [id])
address_id Int
}
model Address {
id Int @id @default(autoincrement())
city String
state_code String @db.VarChar(2)
house House[]
}
model Amenity {
id Int @id @default(autoincrement())
amenity_name String
house House @relation(fields: [house_id], references: [id])
house_id String
}
model Room {
id Int @id @default(autoincrement())
room_number String
occupied Boolean @default(false)
house House @relation(fields: [house_id], references: [id])
house_id String
}
我试着添加房子与他们各自的关系,但这是我发现的唯一方法。
createHomeDto.id = uuid();
const {
amenities,
room,
...home
} = createHomeDto;
await this.prisma.house.create({
data: home
});
Object.entries(amenities).map(async ([key, value]) => {
await this.prisma.amenity.create({
data: {
amenity_name: value.amenity_name,
house: {
connect: {
id: home.id
}
}
}
})
});
Object.entries(room).map(async ([key, value]) => {
await this.prisma.room.create({
data: {
room_number: value.room_number,
house: {
connect: {
id: home.id
}
}
}
})
})
有没有更好的解决方案,或者我坚持这样建立我的关系?
1条答案
按热度按时间tjrkku2a1#
它回答了你的问题吗?
https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#nested-writes
编辑:如果你向下滚动这个页面,你会看到如何在create语句中创建createMany。看看下面的例子: