How to set up one to many self recursive create? #284
-
model location {
id Int @id @default(autoincrement())
location_code String @unique @db.VarChar(255)
location_name String @db.VarChar(255)
parent_loaction AssetLocation? @relation("parentChildren", fields: [parent_id], references: [id])
parent_id Int?
children_location AssetLocation[] @relation("parentChildren")
} I've got a model like this, and in the create function, I have to set up parent_id in the params, but it resulting errors:
my code: client
.asset_location()
.create(
payload.location_code,
payload.location_name,
vec![db::asset_location::parent_id::set(payload.parent_id)],
)
.exec()
.await? |
Beta Was this translation helpful? Give feedback.
Answered by
Brendonovich
Mar 22, 2023
Replies: 2 comments 3 replies
-
Hmm, I can't seem to reproduce this using this code: model location {
id Int @id @default(autoincrement())
location_code String @unique
location_name String
parent_loaction location? @relation("parentChildren", fields: [parent_id], references: [id])
parent_id Int?
children_location location[] @relation("parentChildren")
} let one = client
.location()
.create("1".to_string(), "one".to_string(), vec![])
.exec()
.await?;
client
.location()
.create(
"2".to_string(),
"two".to_string(),
vec![location::parent_id::set(Some(one.id))],
)
.exec()
.await?; Would you mind creating a small reproduction repo? |
Beta Was this translation helpful? Give feedback.
3 replies
-
Ah if you're not trying to connect the record to itself then the problem is definitely the combination of |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Dennis-Zhang-SH
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah if you're not trying to connect the record to itself then the problem is definitely the combination of
asset::connect
andparent_id::set
. That's something I've been thinking about recently and I'll probably separatecreate
intocreate
andcreate_unchecked
which will enforce this at a type-level.