Python 终端中的迁移错误,can';t在Django数据库中添加数据/创建表
更新#2-12月13日(仍需要帮助) 该项目的Github回购协议: 从我对终端的理解来看,在我尝试Python 终端中的迁移错误,can';t在Django数据库中添加数据/创建表,python,django,Python,Django,更新#2-12月13日(仍需要帮助) 该项目的Github回购协议: 从我对终端的理解来看,在我尝试/manage.py makemigrations blog和/manage.py migrate blog之后,它似乎一直在引用以前的迁移 问题可能源于不可为空字段的旧错误,当时我错误地将字符串放入名为order=models.PositiveSmallIntegerField()的字段中,该字段本应为整数。现在,我想添加一个名为rank的新字段,我经历了./manage.py makemigr
/manage.py makemigrations blog
和/manage.py migrate blog
之后,它似乎一直在引用以前的迁移
问题可能源于不可为空字段的旧错误,当时我错误地将字符串放入名为order=models.PositiveSmallIntegerField()
的字段中,该字段本应为整数。现在,我想添加一个名为rank的新字段,我经历了./manage.py makemigrations blog和./manage.py migrate blog的过程,但我在终端中不断得到提示:
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
Operations to perform:
Apply all migrations: blog
Running migrations:
**Applying blog.0007_auto_20141212_1622...Traceback (most recent call last):**
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.7/site-packages/django/core/management/commands/migrate.py", line 160, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 63, in migrate
self.apply_migration(migration, fake=fake)
File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 97, in apply_migration
migration.apply(project_state, schema_editor)
File "/Library/Python/2.7/site-packages/django/db/migrations/migration.py", line 107, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File "/Library/Python/2.7/site-packages/django/db/migrations/operations/fields.py", line 139, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/Library/Python/2.7/site-packages/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 "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/schema.py", line 190, in _alter_field
self._remake_table(model, alter_fields=[(old_field, new_field)])
File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/schema.py", line 135, in _remake_table
self.quote_name(model._meta.db_table),
File "/Library/Python/2.7/site-packages/django/db/backends/schema.py", line 99, in execute
cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py", line 485, in execute
return Database.Cursor.execute(self, query, params)
**django.db.utils.IntegrityError: column order is not unique**
当我运行python manage.py时,我会在终端中得到一长串东西:
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
Operations to perform:
Apply all migrations: blog
Running migrations:
**Applying blog.0007_auto_20141212_1622...Traceback (most recent call last):**
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.7/site-packages/django/core/management/commands/migrate.py", line 160, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 63, in migrate
self.apply_migration(migration, fake=fake)
File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 97, in apply_migration
migration.apply(project_state, schema_editor)
File "/Library/Python/2.7/site-packages/django/db/migrations/migration.py", line 107, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File "/Library/Python/2.7/site-packages/django/db/migrations/operations/fields.py", line 139, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/Library/Python/2.7/site-packages/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 "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/schema.py", line 190, in _alter_field
self._remake_table(model, alter_fields=[(old_field, new_field)])
File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/schema.py", line 135, in _remake_table
self.quote_name(model._meta.db_table),
File "/Library/Python/2.7/site-packages/django/db/backends/schema.py", line 99, in execute
cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py", line 485, in execute
return Database.Cursor.execute(self, query, params)
**django.db.utils.IntegrityError: column order is not unique**
运行
makemigrations
django后,在字段定义中放入“http://”,创建错误的迁移
检查您的migrations
目录,编辑最后一个目录,并手动从字段定义中删除“http://”
编辑:
迁移0007在字段order
上创建一个唯一的constraint,但您的数据库已经包含重复项,因此迁移无法成功
您的情况完全不一致,因为您的系统正在开发中(即尚未发布),最好删除所有迁移,然后再次删除db a运行makemigrations
order = models.PositiveSmallIntegerField()
表示整型字段
和非文本或字符字段
您给出了“http://”
,它是字符串
将此值更改为默认值,如0
或0000
任何值
然后再次运行迁移
“我想知道如何在终端中消除它。”-你最好想知道如何解决问题,然后它就会离开你的终端这里有什么问题?您是否询问如何清除终端的内容?如果是这样的话,
clear
I每次运行/manage.py migrate blog时都会出现此错误。我应该在字段中输入什么?只需将default='http://'
更改为default=0
或在模型定义中输入的任何其他值。您可以共享上次的迁移代码和使用的数据库吗?是的,我刚刚更新了关于Stackoverflow的问题,并添加了一个指向Github回购的链接,如下所示: