【问题标题】:How do I fix my Django "ValueError: Found wrong number (0) of constraints" migration error?如何修复我的 Django “ValueError: Found wrong number (0) of constraints”迁移错误?
【发布时间】:2020-01-03 22:45:34
【问题描述】:

我正在使用 Python 3.7 和 Django。我尝试了这个解决方案——Received "ValueError: Found wrong number (0) of constraints for ..." during Django migration,但仍然遇到同样的错误。我的一项迁移遇到了麻烦。我最近更新了我的模型的独特约束...

class ArticleSumStatByHour(models.Model):
    total_score = models.DecimalField(default=0, max_digits=12, decimal_places=2, null=False)
    total_seconds_to_reach_fp = models.DecimalField(default=0, max_digits=12, decimal_places=2, null=False)
    num_articles = models.IntegerField(default=0, null=False)
    hour_of_day = IntegerField(
        null=False,
        validators=[
            MaxValueValidator(23),
            MinValueValidator(0)
        ]
    )
    index = models.FloatField(default=0)
    website = models.ForeignKey(website, on_delete=models.CASCADE, related_name='articlesumstatbyhoursub')

        class Meta:
            unique_together = ("hour_of_day","website")

这是我的迁移,

...
class Migration(migrations.Migration):

    dependencies = [
        ('articlesum', '0032_auto_20190808_1452'),
    ]

    operations = [
        migrations.AlterField(
            model_name='articlesumstatbyhour',
            name='website',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='articlesumstatbyhoursub', to='articlesum.website'),
        ),
        migrations.AlterUniqueTogether(
            name='articlesumstatbyhour',
            unique_together={('hour_of_day','website')},
        ),
      ]

但是当我运行迁移时,我收到这个令人费解的错误,抱怨“ValueError: Found wrong number (0) of constraints”...

(venv) localhost:articlesum_project davea$ python manage.py migrate articlesum
Operations to perform:
  Apply all migrations: articlesum
Running migrations:
  Applying articlesum.0033_auto_20190830_1128...Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 203, in handle
    fake_initial=fake_initial,
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/db/migrations/operations/models.py", line 514, in database_forwards
    getattr(new_model._meta, self.option_name, set()),
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 356, in alter_unique_together
    self._delete_composed_index(model, fields, {'unique': True}, self.sql_delete_unique)
  File "/Users/davea/Documents/workspace/articlesum_project/venv/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 385, in _delete_composed_index
    ", ".join(columns),
ValueError: Found wrong number (0) of constraints for articlesum_articlesumstatbyhour(website_id, elapsed_time_in_seconds, hour_of_day)

为什么迁移无法创建我的唯一约束?

【问题讨论】:

  • 我在迁移中缺少一组括号,可能是因为您使用的是元组。试试unique_together = [["hour_of_day", "website"]]unique_together = ["hour_of_day", "website"]
  • 感谢您的建议,但遗憾的是这些方法都不起作用(都产生了相同的错误。)
  • 在添加此限制之前是否有其他限制?
  • 不确定——我从离开该项目的其他人那里继承了这个代码库。有没有办法检查这种事情?
  • 是的,挖掘以前的迁移文件。我认为 Django 没有正确删除约束是一个悬而未决的问题。因此,您可能应该自己创建一个迁移文件,在该文件中删除先前的约束,然后再添加新的约束。

标签: django python-3.x migration unique-constraint


【解决方案1】:

似乎当前迁移正在寻找您的数据库当前没有的先前索引。因此,您将需要设置了最后一个唯一索引的 db 表。

每次更改表上的索引时,它都会检查其先前的索引并将其删除。在您的情况下,它无法获取以前的索引。

解决方案- 1.您可以手动生成它 2.或者恢复到使用以前索引的代码并迁移。最后在代码中更改为新索引并运行迁移。(要处理的 django_migration 文件)

【讨论】:

    猜你喜欢
    • 2017-05-28
    • 2015-08-19
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 2019-06-26
    • 2020-02-16
    • 2018-01-08
    • 2018-03-14
    相关资源
    最近更新 更多