我有一个Django模型SessionType
,其定义类似于以下内容:
from django import models
class SessionType(models.Model):
class Meta:
ordering = ['title']
title = models.CharField(max_length=255, unique=True)
起初,unive=True
约束并不存在;我刚刚添加了它,并运行了pythonmanage.pymakaligrations
。这导致了以下迁移(0163_auto_20180627_1309.py
):
# -*- coding: utf-8 -*-
# Generated by Django 1.11.9 on 2018-06-27 20:09
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('lucy_web', '0162_merge_20180531_0009'),
]
operations = [
migrations.AlterField(
model_name='sessiontype',
name='title',
field=models.CharField(max_length=255, unique=True),
),
]
然而,当我尝试< code > python manage . py migrate 时,我得到以下错误:
django.db.utils.Integrity错误:重复的键值违反了唯一约束“django_migrations_pkey”详细信息:键 (id)=(326) 已存在。
以下是命令和完整回溯:
(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py makemigrations
Migrations for 'lucy_web':
lucy_web/migrations/0163_auto_20180627_1309.py
- Alter field title on sessiontype
(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auditlog, auth, contenttypes, defender, lucy_web, oauth2_provider, otp_static, otp_totp, sessions, two_factor
Running migrations:
Applying lucy_web.0163_auto_20180627_1309...Traceback (most recent call last):
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey"
DETAIL: Key (id)=(326) already exists.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 28, in <module>
execute_from_command_line(sys.argv)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/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/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/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/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/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/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 250, in apply_migration
self.recorder.record_applied(migration.app_label, migration.name)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 73, in record_applied
self.migration_qs.create(app=app, name=name)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/models/query.py", line 394, in create
obj.save(force_insert=True, using=self.db)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/models/base.py", line 808, in save
force_update=force_update, update_fields=update_fields)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/models/base.py", line 838, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/models/base.py", line 924, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/models/base.py", line 963, in _do_insert
using=using, raw=raw)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/models/query.py", line 1076, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1112, in execute_sql
cursor.execute(sql, params)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey"
DETAIL: Key (id)=(326) already exists.
查看数据库,我看到有另一个主键为326的迁移:
该迁移(0161_auto_20180530_2140.py
)还包含会话类型
模型上的 AlterField
操作:
# -*- coding: utf-8 -*-
# Generated by Django 1.11.9 on 2018-05-31 04:40
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('lucy_web', '0160_merge_20180524_1507'),
]
operations = [
migrations.AlterField(
model_name='sessiontype',
name='description',
field=models.TextField(blank=True),
),
migrations.AlterField(
model_name='sessiontype',
name='short_description',
field=models.TextField(blank=True),
),
]
在django db迁移因postgres而失败之后,我尝试运行命令
ALTER SEQUENCE django_migrations_id_seq RESTART WITH 329;
像这样:
然而,现在当我尝试迁移时,我遇到另一个错误,即某个唯一约束已经存在:
(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auditlog, auth, contenttypes, defender, lucy_web, oauth2_provider, otp_static, otp_totp, sessions, two_factor
Running migrations:
Applying lucy_web.0163_auto_20180627_1309...Traceback (most recent call last):
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "lucy_web_sessiontype_title_c207e4f8_uniq" already exists
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 28, in <module>
execute_from_command_line(sys.argv)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/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/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/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/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/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/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/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/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 221, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 515, in alter_field
old_db_params, new_db_params, strict)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/backends/postgresql/schema.py", line 112, in _alter_field
new_db_params, strict,
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 719, in _alter_field
self.execute(self._create_unique_sql(model, [new_field.column]))
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 120, in execute
cursor.execute(sql, params)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "lucy_web_sessiontype_title_c207e4f8_uniq" already exists
我该如何解决这个问题?我是否应该删除该唯一约束并重新运行迁移?
重新启动索引后,我确实简单地删除/删除了给出“已存在”错误的约束和索引,并设法迁移:
(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auditlog, auth, contenttypes, defender, lucy_web, oauth2_provider, otp_static, otp_totp, sessions, two_factor
Running migrations:
Applying lucy_web.0163_auto_20180627_1309... OK
重复运行migrate命令,直到错误消失。每次尝试时,它都尝试增加id(pk)值。一旦增量达到现有id的最大值,命令将成功运行。
此修复程序适用于我的后greSQL数据库
开放式丁戈外壳
python manage.py shell
运行以下Python代码重置id
from django.db import connections
query = "SELECT setval('django_migrations_id_seq', (SELECT MAX(id) FROM django_migrations))"
cursor = connections['default'].cursor()
cursor.execute(query)
row = cursor.fetchone()
遇到了一点问题,当我运行''时,我收到了上述错误消息,我正在一个相当旧的站点上工作。它'运行带有postgres DB的django 1.2.6。 运行没有安装南,我设法让它工作。运行pythonmanage.py模式迁移-初始contact_enquiries运行良好并要求我迁移。然后我运行pythonmanage.py迁移contact_enquiries然后我得到了与上述相同的错误。 它没有
我在django应用程序中创建了一个模型,并从pgadmin将数据填充到表中,但现在当我试图从应用程序创建记录时,它抛出了这个完整性错误: 重复的键值违反了唯一约束“packsapp_foo_pkey” 详细信息:键(id)=(4)已经存在。 这是models.py 我是否总是必须从应用程序本身插入数据? Views.py
问题内容: 我正在跟着我先前提出的一个问题,在这个问题中,我试图寻求从愚蠢/编写不佳的mysql查询到postgresql的转换。我相信我成功了。无论如何,我正在使用从mysql数据库手动移动到postgres数据库的数据。我正在使用如下查询: 我有理由相信这很好。但是,这导致了新问题。尝试提交时,我从django收到一条错误,指出: 我已经看过这里发布的一些回复,但是我还没有找到解决我的问题的方
我必须交换同一表的不同行的属性。 有一列“reference_id”在 DB 中具有唯一的约束。 代码: A 级- B级- 异常跟踪:
我有一个笑话模型: 现在,当我试图迁移最后一行时,我得到了错误。基本上,我想将一个用户链接到Joke对象,因为我已经有了一个数据库,所以我希望默认值为1,这是管理员用户的id(我检查过了...).Makemigrations工作正常,但是当我尝试迁移时,我得到了这个: 我真的不明白怎么了。有什么想法吗?