Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only validate FKs when query changes them #99

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ function addFkValidation(writer: CodeBlockWriter, model: Model) {
whereUnique = `{ ${dependentModelField.relationToFields?.join("_")}: { ${dependentModelField.relationToFields?.map((_field, idx) => `${_field}: record.${dependentModelField.relationFromFields?.at(idx)}`).join(", ")} } }`;
}

let condition = `record.${dependentModelField.relationFromFields?.at(0)} !== undefined`;
let condition = `query.data.${dependentModelField.relationFromFields?.at(0)} !== undefined`;
if (!dependentModelField.isRequired)
condition += ` && record.${dependentModelField.relationFromFields?.at(0)} !== null`;

Expand Down
24 changes: 12 additions & 12 deletions packages/usage/src/prisma/prisma-idb/prisma-idb-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3856,11 +3856,11 @@ class UserGroupIDBClass extends BaseIDBModelClass<"UserGroup"> {
);
}
}
if (record.groupId !== undefined) {
if (query.data.groupId !== undefined) {
const related = await this.client.group.findUnique({ where: { id: record.groupId } }, tx);
if (!related) throw new Error("Related record not found");
}
if (record.userId !== undefined) {
if (query.data.userId !== undefined) {
const related = await this.client.user.findUnique({ where: { id: record.userId } }, tx);
if (!related) throw new Error("Related record not found");
}
Expand Down Expand Up @@ -4472,7 +4472,7 @@ class ProfileIDBClass extends BaseIDBModelClass<"Profile"> {
);
}
}
if (record.userId !== undefined) {
if (query.data.userId !== undefined) {
const related = await this.client.user.findUnique({ where: { id: record.userId } }, tx);
if (!related) throw new Error("Related record not found");
}
Expand Down Expand Up @@ -5408,7 +5408,7 @@ class PostIDBClass extends BaseIDBModelClass<"Post"> {
);
}
}
if (record.authorId !== undefined && record.authorId !== null) {
if (query.data.authorId !== undefined && record.authorId !== null) {
const related = await this.client.user.findUnique({ where: { id: record.authorId } }, tx);
if (!related) throw new Error("Related record not found");
}
Expand Down Expand Up @@ -6192,11 +6192,11 @@ class CommentIDBClass extends BaseIDBModelClass<"Comment"> {
);
}
}
if (record.postId !== undefined) {
if (query.data.postId !== undefined) {
const related = await this.client.post.findUnique({ where: { id: record.postId } }, tx);
if (!related) throw new Error("Related record not found");
}
if (record.userId !== undefined) {
if (query.data.userId !== undefined) {
const related = await this.client.user.findUnique({ where: { id: record.userId } }, tx);
if (!related) throw new Error("Related record not found");
}
Expand Down Expand Up @@ -7922,14 +7922,14 @@ class FatherIDBClass extends BaseIDBModelClass<"Father"> {
record.userId = null;
}
}
if (record.motherFirstName !== undefined) {
if (query.data.motherFirstName !== undefined) {
const related = await this.client.mother.findUnique(
{ where: { firstName_lastName: { firstName: record.motherFirstName, lastName: record.motherLastName } } },
tx,
);
if (!related) throw new Error("Related record not found");
}
if (record.userId !== undefined && record.userId !== null) {
if (query.data.userId !== undefined && record.userId !== null) {
const related = await this.client.user.findUnique({ where: { id: record.userId } }, tx);
if (!related) throw new Error("Related record not found");
}
Expand Down Expand Up @@ -9156,7 +9156,7 @@ class MotherIDBClass extends BaseIDBModelClass<"Mother"> {
record.userId = null;
}
}
if (record.userId !== undefined && record.userId !== null) {
if (query.data.userId !== undefined && record.userId !== null) {
const related = await this.client.user.findUnique({ where: { id: record.userId } }, tx);
if (!related) throw new Error("Related record not found");
}
Expand Down Expand Up @@ -10283,18 +10283,18 @@ class ChildIDBClass extends BaseIDBModelClass<"Child"> {
);
}
}
if (record.userId !== undefined && record.userId !== null) {
if (query.data.userId !== undefined && record.userId !== null) {
const related = await this.client.user.findUnique({ where: { id: record.userId } }, tx);
if (!related) throw new Error("Related record not found");
}
if (record.fatherFirstName !== undefined) {
if (query.data.fatherFirstName !== undefined) {
const related = await this.client.father.findUnique(
{ where: { firstName_lastName: { firstName: record.fatherFirstName, lastName: record.fatherLastName } } },
tx,
);
if (!related) throw new Error("Related record not found");
}
if (record.motherFirstName !== undefined) {
if (query.data.motherFirstName !== undefined) {
const related = await this.client.mother.findUnique(
{ where: { firstName_lastName: { firstName: record.motherFirstName, lastName: record.motherLastName } } },
tx,
Expand Down
Loading