添加 "Meta" class 后 Django 数据库迁移停止
Django database migrations stop with addition of "Meta" class
我已经创建了一个 Django 应用程序并且我有一个这样的模型:
class Company(models.Model):
name = models.TextField(max_length = 200)
当我 运行 python3 manage.py makemigrations appname
时,一切都按预期工作,并创建了 class 的迁移文件。
如果模型中的 class 也定义了 Meta
class,就会出现问题:
class Company(models.Model):
name = models.TextField(max_length = 200)
class Meta:
app_label = "Companies"
现在,如果我 运行 makemigrations
命令,我会收到一条 "no changes detected" 消息,并且不会生成任何迁移文件:
> python3 manage.py makemigrations myApp
No changes detected in app 'appname'
或者,更糟糕的是,如果具有 Meta
class 的模型没有依赖项(外键),则会创建一个将完全删除 model/table 的迁移。
或者,如果存在依赖关系(来自另一个模型的外键引用),我会收到如下错误:
matcher.OtherTable.foreignkeyref: (fields.E300) Field defines a relation with model 'Company', which is either not installed, or is abstract.
所有定义了 Meta
class 的模型都不会生成迁移,无论是初始迁移还是出于某种原因的任何后续迁移。 类 之后添加的 Meta 将在下一次迁移时删除。就好像它们根本不存在一样。
我在 Mac OS 10.15.5.
上使用 Python 3.8.3 和 Django 3.0.6
All models with a Meta
class defined will not generate a migration, either an initial one or any subsequent migration for some reason. Classes that have Meta
added after the fact will be removed at the next migration.
好吧,问题是你改变了 app_label
,现在你说这个模型不属于它最初所属的应用程序,而是属于其他地方。因此,对于那个应用程序,模型有 "vanished",而对于一个新应用程序(此处为 Companies
),它出现了 "out of thin air"。正如 documentation 中指定的那样,在以下情况下使用 app_label
:
If a model is defined outside of an application in INSTALLED_APPS, it must declare which app it belongs to (…).
很少指定 app_label
。如果您不指定它,它将采用定义它的应用程序的名称。
看起来您覆盖了错误的设置。根据您传递的值,您可能希望指定 verbose_name_plural
option [Django-doc]:
class Company(models.Model):
name = models.TextField(max_length = 200)
class Meta:
<b>verbose_name_plural</b> = "Companies"
我已经创建了一个 Django 应用程序并且我有一个这样的模型:
class Company(models.Model):
name = models.TextField(max_length = 200)
当我 运行 python3 manage.py makemigrations appname
时,一切都按预期工作,并创建了 class 的迁移文件。
如果模型中的 class 也定义了 Meta
class,就会出现问题:
class Company(models.Model):
name = models.TextField(max_length = 200)
class Meta:
app_label = "Companies"
现在,如果我 运行 makemigrations
命令,我会收到一条 "no changes detected" 消息,并且不会生成任何迁移文件:
> python3 manage.py makemigrations myApp
No changes detected in app 'appname'
或者,更糟糕的是,如果具有 Meta
class 的模型没有依赖项(外键),则会创建一个将完全删除 model/table 的迁移。
或者,如果存在依赖关系(来自另一个模型的外键引用),我会收到如下错误:
matcher.OtherTable.foreignkeyref: (fields.E300) Field defines a relation with model 'Company', which is either not installed, or is abstract.
所有定义了 Meta
class 的模型都不会生成迁移,无论是初始迁移还是出于某种原因的任何后续迁移。 类 之后添加的 Meta 将在下一次迁移时删除。就好像它们根本不存在一样。
我在 Mac OS 10.15.5.
上使用 Python 3.8.3 和 Django 3.0.6All models with a
Meta
class defined will not generate a migration, either an initial one or any subsequent migration for some reason. Classes that haveMeta
added after the fact will be removed at the next migration.
好吧,问题是你改变了 app_label
,现在你说这个模型不属于它最初所属的应用程序,而是属于其他地方。因此,对于那个应用程序,模型有 "vanished",而对于一个新应用程序(此处为 Companies
),它出现了 "out of thin air"。正如 documentation 中指定的那样,在以下情况下使用 app_label
:
If a model is defined outside of an application in INSTALLED_APPS, it must declare which app it belongs to (…).
很少指定 app_label
。如果您不指定它,它将采用定义它的应用程序的名称。
看起来您覆盖了错误的设置。根据您传递的值,您可能希望指定 verbose_name_plural
option [Django-doc]:
class Company(models.Model):
name = models.TextField(max_length = 200)
class Meta:
<b>verbose_name_plural</b> = "Companies"