Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend/db): Keep `CreditTransaction` entries on `User` delete (#10917)

Reinier van der Leer6a86e70fd6fc
diff --git a/autogpt_platform/backend/migrations/20250912164006_keep_credit_transactions_on_user_delete/migration.sql b/autogpt_platform/backend/migrations/20250912164006_keep_credit_transactions_on_user_delete/migration.sql
new file mode 100644
index 0000000000..4e5cbac6c4
--- /dev/null
+++ b/autogpt_platform/backend/migrations/20250912164006_keep_credit_transactions_on_user_delete/migration.sql
@@ -0,0 +1,3 @@
+-- Re-create foreign key CreditTransaction <- User with ON DELETE NO ACTION
+ALTER TABLE "CreditTransaction" DROP CONSTRAINT "CreditTransaction_userId_fkey";
+ALTER TABLE "CreditTransaction" ADD CONSTRAINT "CreditTransaction_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE NO ACTION ON UPDATE CASCADE;
diff --git a/autogpt_platform/backend/schema.prisma b/autogpt_platform/backend/schema.prisma
index d3945659ee..48eae403d0 100644
--- a/autogpt_platform/backend/schema.prisma
+++ b/autogpt_platform/backend/schema.prisma
@@ -528,7 +528,7 @@ model CreditTransaction {
createdAt DateTime @default(now())
userId String
- User User @relation(fields: [userId], references: [id], onDelete: Cascade)
+ User User? @relation(fields: [userId], references: [id], onDelete: NoAction)
amount Int
type CreditTransactionType