Skip to content

Commit

Permalink
fix: only validate FKs when query changes them
Browse files Browse the repository at this point in the history
  • Loading branch information
WhyAsh5114 committed Dec 30, 2024
1 parent 45fd4de commit fbd70c6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
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

0 comments on commit fbd70c6

Please sign in to comment.