prisma: Improve error when updating a non-existent nested record
Node.js version: v12.13.0 Prisma2 version: prisma2@2.0.0-preview017.2
Schema:
generator photon {
provider = "photonjs"
}
datasource db {
provider = "sqlite"
url = "file:{**PATH**}/db.sqlite"
}
model ParentRec { @@map(name: "parent")
id String @default(cuid()) @id
createdAt DateTime @default(now()) @map(name: "created_at")
updatedAt DateTime @updatedAt @map(name: "updated_at")
code String @unique
children ChildRec[]
}
model ChildRec { @@map(name: "child")
id String @default(cuid()) @id
createdAt DateTime @default(now()) @map(name: "created_at")
updatedAt DateTime @updatedAt @map(name: "updated_at")
code String @unique
parent ParentRec @map(name: "parent_id")
}
Code:
const {Photon} = require('@prisma/photon');
async function main() {
const photon = new Photon();
await photon.connect();
try {
const created = await photon.parentRecs.create({
data: {
code: 'parent',
children: {
create: {
code: 'child'
}
}
},
include: {
children: true
}
});
console.log(created);
const updated = await photon.parentRecs.update({
where: {
code: 'parent'
},
data: {
children: {
update: {
where: {
code: 'not_found' // rec with this code doesn't exist
},
data: {
code: 'child_upd'
}
}
}
},
include: {
children: true
}
});
console.log(updated);
} catch (err) {
console.error(err);
} finally {
await photon.disconnect();
}
}
main().then(() => {
console.log('Completed!');
});
Error:
Error:
Invalid `photon.()` invocation in /home/vadim/Projects/Temp/index.js:23:49
Reason: Error occurred during query execution:
InterpretationError("Error for binding \'1\': AssertionError(\"Expected a valid parent ID to be present for nested update to-one case.\")")
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 2
- Comments: 15 (11 by maintainers)
Tested this with:
Tested some different cases here:
Normal update
Throws P2025 with
meta.cause: 'Record to update not found.'
Update with a nested create
Throws P2025 with
meta.cause: "No 'User' record (needed to inline the relation with create on 'Contact' record) was found for a nested create on one-to-one relation 'ContactToUser'."
Update with a nested update
Throws P2016 with
meta.details: "Error for binding '1': AssertionError("Expected a valid parent ID to be present for nested update to-one case.")"
I guess the expectation here is that in the nested update case the error code should be P2025 and a the error message something similar to the message in the nested create case.
Just double-checked this, still running into an unhelpful error. I feel like the confusing part is the “valid parent ID”, it’s a valid ID, it’s just that this ID doesn’t exist.
Throwing the error here is correct. The message is not useful, which is known but unrelated to this issue.
This is expected as the relationship is required. Trying to perform an update on a non-existant record will throw. We need to throw a better here though.