在django中我创建了字段并在之后添加了一些条目

时间:2014-11-05 16:54:26

标签: django

在Django中,我创建了一个名为pincode的字段,并添加了一些条目,如876786876,8768768776。

一段时间后我添加了一个唯一的参数,但它显示了一些错误,所以我从模型中删除了该字段,但在运行时仍显示一些错误

python manage.py migrate

错误:

Operations to perform:
  Synchronize unmigrated apps: haystack
  Apply all migrations: admin, contenttypes, polls, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
  Installing custom SQL...
  Installing indexes...
Running migrations:
  Applying polls.0007_auto_20141104_2055...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/usr/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/core/management/commands/migrate.py", line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/usr/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/db/migrations/executor.py", line 63, in migrate
    self.apply_migration(migration, fake=fake)
  File "/usr/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/db/migrations/executor.py", line 97, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/usr/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/db/migrations/migration.py", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/usr/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/db/migrations/operations/fields.py", line 139, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/usr/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/db/backends/schema.py", line 473, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type, old_db_params, new_db_params, strict)
  File "/usr/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/db/backends/sqlite3/schema.py", line 190, in _alter_field
    self._remake_table(model, alter_fields=[(old_field, new_field)])
  File "/usr/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/db/backends/sqlite3/schema.py", line 135, in _remake_table
    self.quote_name(model._meta.db_table),
  File "/usr/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/db/backends/schema.py", line 99, in execute
    cursor.execute(sql, params)
  File "/usr/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/lib/python2.7/site-packages/Django-1.7.1-py2.7.egg/django/db/backends/sqlite3/base.py", line 485, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: column pincode is not unique

2 个答案:

答案 0 :(得分:1)

您可能会在该列中看到重复的字段。无法添加UNIQUE约束,直到表中的数据已经符合约束。

如果您不关心您的数据,只需截断或删除表格即可。如果您关心数据,则需要识别重复的行并确保相关字段是唯一的。

答案 1 :(得分:-1)

我不介意,删除数据库的文件(如果你正在使用SQLite)和syncdb,有时迁移不要“删除”表列,如果你不想丢失你的信息尝试将它转储到json中并在syncdb与loaddata

之后加载它