From 55214b1dc34808a1512ae03309964d2e3c96e911 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 16 Jul 2025 23:01:31 +0530 Subject: [PATCH] Fix NOT NULL constraint error for stripe_payment_intent_id - Add stripe_payment_intent_id field to WalletTransaction model with proper defaults - Update User.deduct_balance() to handle missing stripe_payment_intent_id gracefully - Update User.add_balance() to handle missing stripe_payment_intent_id gracefully - Use try-catch pattern to handle database schema mismatches - Provide empty string as default for stripe_payment_intent_id when field is required Issue: Database has NOT NULL constraint on stripe_payment_intent_id but code doesn't provide it Solution: Add field with proper defaults and graceful error handling Prevents: 'NOT NULL constraint failed: wallet_wallettransaction.stripe_payment_intent_id' --- authentication/models.py | 50 +++++++++++++++++++++++++++++----------- wallet/models.py | 2 ++ 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/authentication/models.py b/authentication/models.py index 882284b..983abcc 100644 --- a/authentication/models.py +++ b/authentication/models.py @@ -35,13 +35,24 @@ class User(AbstractUser): # Create transaction record from wallet.models import WalletTransaction - WalletTransaction.objects.create( - user=self, - amount=-Decimal(str(amount)), - type='agent_usage', - description=description, - agent_slug=agent_slug - ) + transaction_data = { + 'user': self, + 'amount': -Decimal(str(amount)), + 'type': 'agent_usage', + 'description': description, + 'agent_slug': agent_slug + } + + # Handle stripe_payment_intent_id field if it exists (for agent usage, it's empty/null) + try: + WalletTransaction.objects.create(**transaction_data) + except Exception as e: + # If there's a NOT NULL constraint for stripe_payment_intent_id, provide empty string + if "NOT NULL constraint failed" in str(e) and "stripe_payment_intent_id" in str(e): + transaction_data['stripe_payment_intent_id'] = "" + WalletTransaction.objects.create(**transaction_data) + else: + raise e return True return False @@ -51,13 +62,24 @@ class User(AbstractUser): # Create transaction record from wallet.models import WalletTransaction - WalletTransaction.objects.create( - user=self, - amount=Decimal(str(amount)), - type='top_up', - description=description, - stripe_session_id=stripe_session_id - ) + transaction_data = { + 'user': self, + 'amount': Decimal(str(amount)), + 'type': 'top_up', + 'description': description, + 'stripe_session_id': stripe_session_id + } + + # Handle stripe_payment_intent_id field if it exists (for wallet top-up, it's empty/null) + try: + WalletTransaction.objects.create(**transaction_data) + except Exception as e: + # If there's a NOT NULL constraint for stripe_payment_intent_id, provide empty string + if "NOT NULL constraint failed" in str(e) and "stripe_payment_intent_id" in str(e): + transaction_data['stripe_payment_intent_id'] = "" + WalletTransaction.objects.create(**transaction_data) + else: + raise e class PasswordResetToken(models.Model): diff --git a/wallet/models.py b/wallet/models.py index acaf2da..c898885 100644 --- a/wallet/models.py +++ b/wallet/models.py @@ -19,6 +19,8 @@ class WalletTransaction(models.Model): description = models.TextField() agent_slug = models.CharField(max_length=100, blank=True) stripe_session_id = models.CharField(max_length=200, blank=True) + # Handle stripe_payment_intent_id field if it exists in database (migration issue) + stripe_payment_intent_id = models.CharField(max_length=200, blank=True, null=True, default="") created_at = models.DateTimeField(auto_now_add=True) class Meta: