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'
This commit is contained in:
Claude 2025-07-16 23:01:31 +05:30
parent 65c713ef64
commit 55214b1dc3
2 changed files with 38 additions and 14 deletions

View File

@ -35,13 +35,24 @@ class User(AbstractUser):
# Create transaction record # Create transaction record
from wallet.models import WalletTransaction from wallet.models import WalletTransaction
WalletTransaction.objects.create( transaction_data = {
user=self, 'user': self,
amount=-Decimal(str(amount)), 'amount': -Decimal(str(amount)),
type='agent_usage', 'type': 'agent_usage',
description=description, 'description': description,
agent_slug=agent_slug '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 True
return False return False
@ -51,13 +62,24 @@ class User(AbstractUser):
# Create transaction record # Create transaction record
from wallet.models import WalletTransaction from wallet.models import WalletTransaction
WalletTransaction.objects.create( transaction_data = {
user=self, 'user': self,
amount=Decimal(str(amount)), 'amount': Decimal(str(amount)),
type='top_up', 'type': 'top_up',
description=description, 'description': description,
stripe_session_id=stripe_session_id '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): class PasswordResetToken(models.Model):

View File

@ -19,6 +19,8 @@ class WalletTransaction(models.Model):
description = models.TextField() description = models.TextField()
agent_slug = models.CharField(max_length=100, blank=True) agent_slug = models.CharField(max_length=100, blank=True)
stripe_session_id = models.CharField(max_length=200, 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) created_at = models.DateTimeField(auto_now_add=True)
class Meta: class Meta: