quantum-ai-v3/wallet/models.py
Claude 55214b1dc3 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'
2025-07-16 23:01:31 +05:30

31 lines
1.1 KiB
Python

from django.db import models
from django.contrib.auth import get_user_model
import uuid
User = get_user_model()
class WalletTransaction(models.Model):
TRANSACTION_TYPES = [
('top_up', 'Top Up'),
('agent_usage', 'Agent Usage'),
('refund', 'Refund'),
]
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='wallet_transactions')
amount = models.DecimalField(max_digits=10, decimal_places=2)
type = models.CharField(max_length=20, choices=TRANSACTION_TYPES)
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:
ordering = ['-created_at']
def __str__(self):
return f"{self.user.email} - {self.amount} AED ({self.type})"