Audit and update all foreign key relations in schema.prisma with appropriate cascade/restrict/set-null strategies to prevent orphaned records and FK constraint violations on parent deletion. Changes (RESTRICT → CASCADE): - Agent.userId, Listing.propertyId, Transaction.listingId - Inquiry.listingId, Inquiry.userId, Lead.agentId - Subscription.userId, UsageRecord.subscriptionId - Valuation.propertyId, Review.userId Confirmed correct (no change needed): - Listing.agentId (SetNull), Listing.sellerId (Restrict) - Transaction.buyerId (Restrict), Payment.userId (Restrict) - Payment.transactionId (SetNull), Subscription.planId (Restrict) - PropertyMedia, SavedSearch, RefreshToken, OAuthAccount (CASCADE) Migration: 20260411000000_add_cascade_delete_strategies Co-Authored-By: Paperclip <noreply@paperclip.ing>
60 lines
2.9 KiB
SQL
60 lines
2.9 KiB
SQL
-- DropForeignKey
|
|
ALTER TABLE "Agent" DROP CONSTRAINT "Agent_userId_fkey";
|
|
|
|
-- DropForeignKey
|
|
ALTER TABLE "Listing" DROP CONSTRAINT "Listing_propertyId_fkey";
|
|
|
|
-- DropForeignKey
|
|
ALTER TABLE "Transaction" DROP CONSTRAINT "Transaction_listingId_fkey";
|
|
|
|
-- DropForeignKey
|
|
ALTER TABLE "Inquiry" DROP CONSTRAINT "Inquiry_listingId_fkey";
|
|
|
|
-- DropForeignKey
|
|
ALTER TABLE "Inquiry" DROP CONSTRAINT "Inquiry_userId_fkey";
|
|
|
|
-- DropForeignKey
|
|
ALTER TABLE "Lead" DROP CONSTRAINT "Lead_agentId_fkey";
|
|
|
|
-- DropForeignKey
|
|
ALTER TABLE "Subscription" DROP CONSTRAINT "Subscription_userId_fkey";
|
|
|
|
-- DropForeignKey
|
|
ALTER TABLE "UsageRecord" DROP CONSTRAINT "UsageRecord_subscriptionId_fkey";
|
|
|
|
-- DropForeignKey
|
|
ALTER TABLE "Valuation" DROP CONSTRAINT "Valuation_propertyId_fkey";
|
|
|
|
-- DropForeignKey
|
|
ALTER TABLE "Review" DROP CONSTRAINT "Review_userId_fkey";
|
|
|
|
-- AddForeignKey: Agent.userId -> User.id (CASCADE)
|
|
ALTER TABLE "Agent" ADD CONSTRAINT "Agent_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey: Listing.propertyId -> Property.id (CASCADE)
|
|
ALTER TABLE "Listing" ADD CONSTRAINT "Listing_propertyId_fkey" FOREIGN KEY ("propertyId") REFERENCES "Property"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey: Transaction.listingId -> Listing.id (CASCADE)
|
|
ALTER TABLE "Transaction" ADD CONSTRAINT "Transaction_listingId_fkey" FOREIGN KEY ("listingId") REFERENCES "Listing"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey: Inquiry.listingId -> Listing.id (CASCADE)
|
|
ALTER TABLE "Inquiry" ADD CONSTRAINT "Inquiry_listingId_fkey" FOREIGN KEY ("listingId") REFERENCES "Listing"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey: Inquiry.userId -> User.id (CASCADE)
|
|
ALTER TABLE "Inquiry" ADD CONSTRAINT "Inquiry_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey: Lead.agentId -> Agent.id (CASCADE)
|
|
ALTER TABLE "Lead" ADD CONSTRAINT "Lead_agentId_fkey" FOREIGN KEY ("agentId") REFERENCES "Agent"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey: Subscription.userId -> User.id (CASCADE)
|
|
ALTER TABLE "Subscription" ADD CONSTRAINT "Subscription_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey: UsageRecord.subscriptionId -> Subscription.id (CASCADE)
|
|
ALTER TABLE "UsageRecord" ADD CONSTRAINT "UsageRecord_subscriptionId_fkey" FOREIGN KEY ("subscriptionId") REFERENCES "Subscription"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey: Valuation.propertyId -> Property.id (CASCADE)
|
|
ALTER TABLE "Valuation" ADD CONSTRAINT "Valuation_propertyId_fkey" FOREIGN KEY ("propertyId") REFERENCES "Property"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
|
|
|
-- AddForeignKey: Review.userId -> User.id (CASCADE)
|
|
ALTER TABLE "Review" ADD CONSTRAINT "Review_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|