admin管理员组文章数量:1415467
I have a model called "Setup"
model Setup {
id String @id @default(auto()) @map("_id") @db.ObjectId
userId String? @unique @db.ObjectId
user User? @relation(fields: [userId], references: [id])
contract String[]
legal String[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
In this model i want to store an array like
const contractData = {
id: '729a4839f3dapob44zt2b4b1',
name: 'Example Name',
text: 'Example Text'
}
so in my above model "Setup" i want to store the contractData
prisma.setup.create({
data: {
userId: '6399bc74426f71f2da6e316c',
personal: [],
contract: contractData,
legal: []
}
})
Unfortunately, this not work.
How can i define an Object for contract and store this in my database?
I have a model called "Setup"
model Setup {
id String @id @default(auto()) @map("_id") @db.ObjectId
userId String? @unique @db.ObjectId
user User? @relation(fields: [userId], references: [id])
contract String[]
legal String[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
In this model i want to store an array like
const contractData = {
id: '729a4839f3dapob44zt2b4b1',
name: 'Example Name',
text: 'Example Text'
}
so in my above model "Setup" i want to store the contractData
prisma.setup.create({
data: {
userId: '6399bc74426f71f2da6e316c',
personal: [],
contract: contractData,
legal: []
}
})
Unfortunately, this not work.
How can i define an Object for contract and store this in my database?
Share Improve this question asked Dec 16, 2022 at 12:19 user19540948user195409483 Answers
Reset to default 2If you want to store a raw JSON, check out this guide: https://www.prisma.io/docs/concepts/ponents/prisma-client/working-with-fields/working-with-json-fields
You will want to use the Json
in Prisma in order to be able to store a raw JSON object (or multiple JSON objects as an array.)
model Setup {
id String @id @default(auto()) @map("_id") @db.ObjectId
userId String? @unique @db.ObjectId
user User? @relation(fields: [userId], references: [id])
contract Json[]
legal String[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Your Prisma prisma.setup.create
query would basically be the exact same. Note that querying for what's in this JSON will be trickier - I would remend creating a new model and then connecting it to the Setup
model, but that if that isn't an option you can still perform some limited queries on the contract
field.
Docs: https://www.prisma.io/docs/concepts/ponents/prisma-client/working-with-fields/working-with-json-fields#filter-on-a-json-field
this is old,
quick answer is, it will be best to create a new model for your contractData object and then link to the parent using Prisma's relation.
JSON would be extremely difficult to parse, and you can't just manufacture a data type like "object".
Lastly, [] is used to indicate a many relationship when suffixed to another model name, not to be confused with the List you have in Js or Ts
In order to store an object in the contract field, you will need to change the type of the contract field from String[] to Object[].
model Setup {
id String @id @default(auto()) @map("_id") @db.ObjectId
userId String? @unique @db.ObjectId
user User? @relation(fields: [userId], references: [id])
contract Object[]
legal String[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
store an object in the contract field like
prisma.setup.create({
data: {
userId: '6399bc74426f71f2da6e316c',
personal: [],
contract: [contractData],
legal: []
}
})
contract
field is now an array, so you will need to pass the object as an element of the array, like [contractData]
本文标签: javascriptHow do i store custom object in prisma schemaStack Overflow
版权声明:本文标题:javascript - How do i store custom object in prisma schema? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745157892a2645286.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论