简体   繁体   English

Django 重复检测冗余迁移

[英]Django detecting redundant migrations repetitively

Background背景

We are working in Python3.4 / Django1.8.4 and we are witnessing a strange phenomenon with respect to our user model, and specifically the timezone field of this model.我们在 Python3.4 / Django1.8.4 中工作,我们正在目睹关于我们的user模型的奇怪现象,特别是该模型的timezone字段。

Every so often when we are making migrations the new migration file will include an operation to alter said timezone field, but all of the attributes that are included in the operation are already set to the same values that the migration is trying to assign!每隔一段时间,当我们进行迁移时,新的迁移文件将包含一个更改所述时区字段的操作,但是操作中包含的所有属性都已经设置为迁移试图分配的相同值!

There are 3 such fields and they are:有 3 个这样的字段,它们是:

1) default - with the value of "UTC" 1) default - 值为"UTC"

2) max_length - with the value of 30 , and 2) max_length - 值为30 ,和

3) choices - a very long array of tuples containing time zone names/values. 3) choices - 包含时区名称/值的非常长的元组数组。

It looks like:看起来像:

choices=[('Africa/Abidjan', 'Africa/Abidjan'), ('Africa/Accra', 'Africa/Accra'), ('Africa/Addis_Ababa', 'Africa/Addis_Ababa'), ... ]

The migration operation always wants to set these 3 properties of the timezone field to the exact same 3 corresponding values, even though they are already set to such values, It is essentially a redundant.迁移操作总是希望将timezone字段的这3个属性设置为完全相同的3个对应值,即使它们已经设置为这样的值, 本质上是多余的。 useless operation.无用的操作。

Sometimes when we run makemigrations there will be no changes to the app, except to this silly field!有时,当我们运行makemigrations时,应用程序不会发生任何变化,除了这个愚蠢的字段!

Questions问题

1) Why is this happening? 1)为什么会这样?

2) How do we prevent this? 2)我们如何防止这种情况? It's annoying that the app thinks migrations are needed when they aren't.令人恼火的是,该应用程序认为在不需要时需要迁移。

Extra Info额外信息

While the same 3 properties of the field are always set to the exact same values, the order that they appear in the operation seems to be non-deterministic (likely because django uses unordered dict instances to store the data which is used to generate the migration file).虽然字段的相同 3 个属性始终设置为完全相同的值,但它们在操作中出现的顺序似乎是不确定的(可能是因为 django 使用无序的dict实例来存储用于生成迁移的数据文件)。

The choices field, as we define it in our model, is dynamically generated when the app is initially run.正如我们在模型中定义的那样, choices字段是在应用程序最初运行时动态生成的。 The (boiled-down) code looks like this: (简化的)代码如下所示:

class MyUser(models.Model):
    f_name = models.CharField(max_length=32398) # Gotta accomodate those crazy south-eastern names haha
    l_name = models.CharField(max_length=94823)

    # ...
    # more fields and stuff etc.
    # ...

    time_zone = models.CharField(default="UTC", choices=TIMEZONE_CHOICES, max_length=30)

The important part there is that choices=TIMEZONE_CHOICES , which is defined earlier as such:重要的部分是choices=TIMEZONE_CHOICES ,它之前定义如下:

import pytz
TIMEZONE_CHOICES = ()
for time_zone in pytz.common_timezones:
    TIMEZONE_CHOICES += ((time_zone, time_zone),)

Just including this information in case it turns out to be relevant.仅包含此信息以防万一它被证明是相关的。

Try to copy the plain list of timezones from pytz to your project, so you are sure that choices does not depends from a third party尝试将时区的普通列表从pytz到您的项目,这样您就可以确定选择不依赖于第三方

You could try using an existing package that will allow you to use a time zone directly as a model field.您可以尝试使用现有的包,它允许您直接将时区用作模型字段。

https://pypi.python.org/pypi/django-timezone-field/ https://pypi.python.org/pypi/django-timezone-field/

class MyModel(models.Model):
    timezone1 = TimeZoneField(default='Europe/London') # defaults supported

Your timezones are probably coming out in different orders on different runs.您的时区可能在不同的运行中以不同的顺序出现。 Try尝试

import pytz
TIMEZONE_CHOICES = ()
for time_zone in pytz.common_timezones:
    TIMEZONE_CHOICES += ((time_zone, time_zone),)
TIMEZONE_CHOICES = sorted(TIMEZONE_CHOICES) # sort list to make it consistent

Also--a minor nit since this only runs at startup--but building up a big tuple with repeated concatenation is probably a lot more expensive than this另外——一个小问题,因为它只在启动时运行——但是建立一个重复连接的大元组可能比这要昂贵得多

import pytz
TIMEZONE_CHOICES = sorted((tz,tz) for tz in pytz.common_timezones)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM