Skip to content

Commit 85021e3

Browse files
committed
Require buyer and seller on UserReview at the schema level.
Match the frontend contract by marking the buyer and seller relations as nullable: false and adding a migration that purges orphaned reviews and applies NOT NULL constraints to buyerId and sellerId. Made-with: Cursor
1 parent 51156ba commit 85021e3

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { MigrationInterface, QueryRunner } from "typeorm";
2+
3+
export class UserReviewBuyerSellerNotNull1770000000000
4+
implements MigrationInterface
5+
{
6+
name = "UserReviewBuyerSellerNotNull1770000000000";
7+
8+
public async up(queryRunner: QueryRunner): Promise<void> {
9+
const orphans: Array<{ id: string }> = await queryRunner.query(
10+
`SELECT "id" FROM "UserReview" WHERE "buyerId" IS NULL OR "sellerId" IS NULL`,
11+
);
12+
if (orphans.length > 0) {
13+
console.log(
14+
`UserReviewBuyerSellerNotNull: removing ${orphans.length} UserReview rows missing buyer or seller before tightening constraints.`,
15+
);
16+
await queryRunner.query(
17+
`DELETE FROM "UserReview" WHERE "buyerId" IS NULL OR "sellerId" IS NULL`,
18+
);
19+
}
20+
21+
await queryRunner.query(
22+
`ALTER TABLE "UserReview" ALTER COLUMN "buyerId" SET NOT NULL`,
23+
);
24+
await queryRunner.query(
25+
`ALTER TABLE "UserReview" ALTER COLUMN "sellerId" SET NOT NULL`,
26+
);
27+
}
28+
29+
public async down(queryRunner: QueryRunner): Promise<void> {
30+
await queryRunner.query(
31+
`ALTER TABLE "UserReview" ALTER COLUMN "sellerId" DROP NOT NULL`,
32+
);
33+
await queryRunner.query(
34+
`ALTER TABLE "UserReview" ALTER COLUMN "buyerId" DROP NOT NULL`,
35+
);
36+
}
37+
}

src/models/UserReviewModel.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ export class UserReviewModel {
2828
@CreateDateColumn({ type: "timestamptz" })
2929
date: Date;
3030

31-
@ManyToOne(() => UserModel, (buyer) => buyer.reviewsWritten)
31+
@ManyToOne(() => UserModel, (buyer) => buyer.reviewsWritten, {
32+
nullable: false,
33+
})
3234
@JoinColumn({ name: "buyerId" })
3335
buyer: UserModel;
3436

35-
@ManyToOne(() => UserModel, (seller) => seller.reviewsReceived)
37+
@ManyToOne(() => UserModel, (seller) => seller.reviewsReceived, {
38+
nullable: false,
39+
})
3640
@JoinColumn({ name: "sellerId" })
3741
seller: UserModel;
3842

0 commit comments

Comments
 (0)