python - django - unique_together 变化 - 生产数据库中有任何危险吗?

标签 python django

我有一个模型:

class MyModel(models.Model):
   name = models.CharField(max_length=10)
   nickname = models.CharField(max_length=10)
   title = models.CharField(max_length=10)
   score = models.CharField(max_length=10)

   class Meta: 
      unique_together = ['name', 'nickname']

unique_together改成

有什么影响
unique_together = ['name', 'title']

在部署此更新之前我应该​​小心吗?超过15万用户同时在线,最坏情况会怎样?

最佳答案

是的,我会小心部署此更改,因为它会影响数据库。 来自 the documentation :

This is a tuple of tuples that must be unique when considered together. It’s used in the Django admin and is enforced at the database level (i.e., the appropriate UNIQUE statements are included in the CREATE TABLE statement).

所以你需要migrate your database更改 UNIQUE 约束。

python manage.py makemigrations myapp
python manage.py migrate myapp

您还需要检查您的模型中是否没有任何现有实例共享该对唯一约束。你可以检查类似的东西:

MyModel.objects.filter(name__exact=models.F(title)).exists()

并修改或删除它们。

您还需要确保每个模型实例实际上都有一个 title 并且不是空字符串(即使这可能不应该发生)。

MyModel.objects.filter(title__exact="")

关于python - django - unique_together 变化 - 生产数据库中有任何危险吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29799533/

相关文章:

Python:从列表转换为字符串

Django 无法级联删除相关的通用外键对象

django - 反向 Django 通用 View ,post_save_redirect;错误 'included urlconf doesnt have any patterns'

python - 对 django 的 urls.py 的更改未在线反射(reflect)

python - 如何为 Python 3 Jupyter Notebook 安装 Plotly?

python - Pandas 在数据框中的字符串旁边获取值

python - 如何在基于DEAP的Python遗传算法中加入淘汰机制

python - 在 Ruby 和 Python 中使用 yield 创建列表

django - 如何将自定义控件添加到 django 管理站点?

Django 扩展错误消息