【问题标题】:Problem with Updating unique together constraint Django更新唯一一起约束Django的问题
【发布时间】:2019-04-14 23:29:18
【问题描述】:

尝试使用 Django-ORM 更新 unqiue_together 约束

迁移时出现以下错误:

File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1664, in <module>
main()
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1658, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1068, in run
pydev_imports.execfile(file, globals, locals)  # execute the script
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Users/arpitkoolwal/DEV/Gateway-26Oct/gateway/gateway/manage.py", line 24, in <module>
execute_from_command_line(sys.argv)
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/db/migrations/operations/models.py", line 536, in database_forwards
getattr(new_model._meta, self.option_name, set()),
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 365, in alter_unique_together
self._delete_composed_index(model, fields, {'unique': True}, self.sql_delete_unique)
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/db/backends/mysql/schema.py", line 88, in _delete_composed_index
return super(DatabaseSchemaEditor, self)._delete_composed_index(model, fields, *args)
File "/Users/arpitkoolwal/DEV/cld/pyenv3/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 394, in _delete_composed_index
", ".join(columns), ValueError: Found wrong number (0) of constraints for test(a, b)

Django:1.11.15

错误单:Django-Offical

谁能告诉我,我们如何使用 Django-ORM 修改现有的唯一约束?

编辑:

型号-

class test(models.Model):
    a = models.CharField(max_length=500, blank=True, null=True)
    b = models.CharField(max_length=500, blank=True, null=True)
    c = models.CharField(max_length=500, blank=True, null=True)
    d = models.CharField(max_length=500, blank=True, null=True)
    class Meta:
     unique_together = ('a', 'b')

迁移代码:

class Migration(migrations.Migration):

dependencies = [
    ('demo', '0230_auto_20181106_1243'),
]

operations = [
    migrations.AlterUniqueTogether(
        name='test',
        unique_together=set([a,b]),
    ),
]

【问题讨论】:

  • 你能发布你的代码吗?
  • @bak2trak 你现在可以检查了吗?
  • 我无法重现它,它对我来说工作正常,您使用的是哪个数据库。我在创建普通表后检查了它,然后添加了这个约束。 (使用默认 sqlite3)
  • 现在,一旦您添加了约束,请尝试删除它。这个问题,我面临着mysql 5.7。不了解sqlite
  • 仍然工作正常。看看这个stackoverflow.com/questions/41623515/…

标签: django python-3.x


【解决方案1】:

我是如何解决这个问题的:

  1. python -m pdb manage.py migrate
  2. 输入up 进入上框。它会去

    ./python3.6/site-packages/django/db/backends/base/schema.py(363)alter_unique_together() -> self._delete_composed_index(model, fields, {'unique': True}, self.sql_delete_unique)

  3. 键入a 以显示变量

通知

old_unique_together = {('a', 'b')}
new_unique_together = {('a', 'b', 'c')}

old_unique_together 不存在,根据 pg \d tablename

  1. 转到旧的迁移文件并找到旧的migrations.AlterUniqueTogether,将它们注释掉,然后再次运行迁移。
  2. ???
  3. 利润。

【讨论】:

  • 谢谢!也一样
【解决方案2】:

我刚刚遇到了这个错误,花了很长时间才弄清楚。我从接受的答案中借用并注释掉了相关迁移文件中最新的AlterUniqueTogether。不确定这是否会导致长期不稳定,但它允许迁移工作。

【讨论】:

    猜你喜欢
    • 2020-11-07
    • 2013-07-04
    • 2022-01-05
    • 2019-09-25
    • 2017-01-10
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 2011-11-12
    相关资源
    最近更新 更多