django - Django 迁移 sqlmigrate
问题描述
我创建了一个迁移文件来更改模型的唯一键约束,以前它在唯一键中有 4 个字段,现在它有 5 个,在运行 migrate 命令后它工作正常。但现在我无法使用 sqlmigrate 生成 sql 查询。
错误是
“ValueError:发现规则集(business_function_id,business_process_id,process_state,status)的约束数量错误(0)”
在我的数据库中,现在的约束是:-
唯一键rule_set_business_function_id_bus_6bda6da3_uniq
( business_function_id
, business_process_id
, process_state
, rule_set_name
, status
),
我做错了什么?
以前的迁移
migrations.CreateModel(
name='RuleSet',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', myghanta.fields.MysqlTimeStampField(auto_now_add=True, help_text='Date and Time of creation')),
('updated_at', myghanta.fields.MysqlTimeStampField(auto_now=True, help_text='Date and Time of modification')),
('process_state', models.CharField(max_length=255)),
('rule_set_name', models.CharField(max_length=255)),
('rule_set_link', models.URLField(max_length=400)),
('status', models.IntegerField(blank=True, choices=[(0, 'Created'), (1, 'Loaded'), (3, 'Validated'), (5, 'InActive'), (None, 'Deleted'), (2, 'Published')], null=True)),
('last_status', models.IntegerField(choices=[(0, 'Created'), (1, 'Loaded'), (3, 'Validated'), (5, 'InActive'), (None, 'Deleted'), (2, 'Published')], default=0)),
('curr_index_name', models.CharField(max_length=255, null=True)),
('prev_index_name', models.CharField(max_length=255, null=True)),
('version', models.PositiveIntegerField(default=0, null=True)),
('published_version', models.PositiveIntegerField(default=0, null=True)),
('published_date', myghanta.fields.MysqlTimeStampField(blank=True, help_text='Date and Time of ruleset last published', null=True)),
('marked_unpublished', models.BooleanField(default=False)),
('business_function', models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='business.BusinessFunction')),
('business_process', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='business.BusinessProcess')),
('updated_by', models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, related_name='last_updated', to=settings.AUTH_USER_MODEL)),
],
options={
'verbose_name': 'Rule Set',
'verbose_name_plural': 'Rule Sets',
'db_table': 'rule_set',
'unique_together': {('business_function', 'business_process', 'process_state', 'status')},
},
)
新的迁移文件
class Migration(migrations.Migration):
dependencies = [
('business', '0001_initial'),
('rules', '0002_auto_20200914_1227'),
]
operations = [
migrations.AlterUniqueTogether(
name='ruleset',
unique_together={('business_function', 'business_process', 'process_state', 'rule_set_name', 'status')},
),
]
追溯
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/sqlmigrate.py", line 30, in execute
return super().execute(*args, **options)
File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/sqlmigrate.py", line 64, in handle
sql_statements = executor.collect_sql(plan)
File "/usr/local/lib/python3.7/site-packages/django/db/migrations/executor.py", line 225, in collect_sql
state = migration.apply(state, schema_editor, collect_sql=True)
File "/usr/local/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 "/usr/local/lib/python3.7/site-packages/django/db/migrations/operations/models.py", line 530, in database_forwards
getattr(new_model._meta, self.option_name, set()),
File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 363, in alter_unique_together
self._delete_composed_index(model, fields, {'unique': True}, self.sql_delete_unique)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/schema.py", line 84, in _delete_composed_index
return super()._delete_composed_index(model, fields, *args)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 397, in _delete_composed_index
", ".join(columns),
ValueError: Found wrong number (0) of constraints for rule_set(business_function_id, business_process_id, process_state, status)
解决方案
推荐阅读
- python - psycopg2 copy_expert - 查找插入的行数
- amazon-dynamodb - dynamodb 从项目中获取二进制文件
- docker - 通过卷 docker 持久存储
- apache-flink - 如何处理 Apache Flink 中的瞬态/应用程序故障?
- python - Python自定义比较器对特定列表进行排序
- python - 查询期间丢失与 MySQL 服务器的连接 - Python、MySql
- php - 当导入的文本包含项目符号时在 PHP 中导入 CSV 文件
- javascript - 如何使用 Nuxt 为不同的 API 服务器设置代理?
- c++ - 我可以在不先找到它的情况下更改标准映射的值吗
- php - Htaccess 重定向规则问题