diff --git a/apps/system_manage/migrations/0008_add_chat_user_token_quota.py b/apps/system_manage/migrations/0008_add_chat_user_token_quota.py index 52853ec39ff..3db09f3b677 100644 --- a/apps/system_manage/migrations/0008_add_chat_user_token_quota.py +++ b/apps/system_manage/migrations/0008_add_chat_user_token_quota.py @@ -10,7 +10,7 @@ def migrate_historical_tokens(apps, schema_editor): ChatUserTokenQuota = apps.get_model('system_manage', 'ChatUserTokenQuota') chat_user_map = { - str(c['id']): c['chat_user_id'] + str(c['id']): str(c['chat_user_id']) for c in Chat.objects.values('id', 'chat_user_id').iterator() } user_totals = {} @@ -24,10 +24,11 @@ def migrate_historical_tokens(apps, schema_editor): user_totals[user_id] = user_totals.get(user_id, 0) + tokens objs = [ - ChatUserTokenQuota(user_id=uid, total_tokens=total) + ChatUserTokenQuota(user_id=uid, total_tokens=total, used_tokens=total) for uid, total in user_totals.items() ] - ChatUserTokenQuota.objects.bulk_create(objs, batch_size=500) + if objs: + ChatUserTokenQuota.objects.bulk_create(objs, batch_size=500) class Migration(migrations.Migration): @@ -51,7 +52,7 @@ class Migration(migrations.Migration): verbose_name="主键id", ), ), - ("user_id", models.UUIDField(unique=True, verbose_name="用户id")), + ("user_id", models.CharField(db_index=True, max_length=128, unique=True, verbose_name="用户id")), ( "quota_type", models.CharField( diff --git a/apps/system_manage/models/chat_user_token_quota.py b/apps/system_manage/models/chat_user_token_quota.py index 34bb8d61d3b..c5fc269bd13 100644 --- a/apps/system_manage/models/chat_user_token_quota.py +++ b/apps/system_manage/models/chat_user_token_quota.py @@ -31,7 +31,7 @@ class ChatUserTokenQuota(AppModelMixin): """ id = models.UUIDField(primary_key=True, max_length=128, default=uuid.uuid7, editable=False, verbose_name="主键id") - user_id = models.UUIDField(max_length=128, unique=True, verbose_name="用户id") + user_id = models.CharField(max_length=128, unique=True, verbose_name="用户id", db_index=True) quota_type = models.CharField(max_length=20, choices=QuotaType.choices, default=QuotaType.UNLIMITED, verbose_name="配额模式")