相关文章推荐
强健的沙发  ·  IndexOutOfRangeExcepti ...·  1 月前    · 
干练的火锅  ·  飞猪 - 开放平台·  3 月前    · 
数据库与django makemigrations不同步django.db.utils.OperationalError: (1060, "Duplicate column name 'azi1'") 2018-10-25 16:54:59

产生背景:由于Models修改过于频繁,而且多对多关系建了又重建,造成部分字段在数据库中已经存在,而django认为还没有创建,继续向数据库创建这个字段,导致错误,原因是数据库不同步。

Microsoft Windows [版本 10.0.17134.345]
(c) 2018 Microsoft Corporation。保留所有权利。

(venv) C:\Users\Administrator\PycharmProjects\cellsmap>py manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, dbbackend, sessions
Running migrations:
Applying dbbackend.0010_auto_20181025_1256...Traceback (most recent call last):
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute
return self.cursor.execute(query, args)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
res = self._query(query)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
rowcount = self._do_query(q)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
db.query(q)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\MySQLdb\connections.py", line 277, in query
_mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1060, "Duplicate column name 'azi1'")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
utility.execute()
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\core\management\__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\core\management\base.py", line 335, in execute
output = self.handle(*args, **options)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\core\management\commands\migrate.py", line 200, in handle
fake_initial=fake_initial,
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\db\migrations\migration.py", line 122, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
field,
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\db\backends\mysql\schema.py", line 40, in add_field
super().add_field(model, field)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\db\backends\base\schema.py", line 437, in add_field
self.execute(sql, params)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\db\backends\base\schema.py", line 133, in execute
cursor.execute(sql, params)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute
return self.cursor.execute(query, args)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
res = self._query(query)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
rowcount = self._do_query(q)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
db.query(q)
File "C:\Users\Administrator\PycharmProjects\cellsmap\venv\lib\site-packages\MySQLdb\connections.py", line 277, in query
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (1060, "Duplicate column name 'azi1'")

根据stackoverflow上找到解决方案,执行:

python manage.py migrate myapp --fake

https://blog.csdn.net/huanhuanq1209/article/details/77884014

django 迁移模型常见错误 django . db . utils .Intern al Error : (1050, “Table ‘ django _content_type’ al ready exists”)” 这个问题经常出现在迁移模型时报错,表示该表已经存在,说明之前进行过模型的迁移操作。 一般情况下出现这种错误,是因为在迁移过后进行了数据表的修改【字段的修改】,而再次进行数据表的迁移便会报错,因为之前该表已经存在,重复迁移。 解决办法: 1、先删除模块中迁移时生成的文件xxx_.initi al .py文件 如: 0001_initi al .py文件 注意:只删除该文件 2、然后依次输入以下命令 django . db . utils . Operation al Error :( 1060 ," Dupli cat e column name'user_id'") 键入命令行 python manage.py db shell 进入数据表 django _ migration s, 删除对应的app字段 重新运行(可以尝试删除项目app-> migration s 删除当前文件下的除了__init__.... 这个时候的解决办法是 删除掉所有的 migration s 下面除了init 文件的所有文件。在 django 的model中进行 数据库 字段的添加时,如果出现。出错的原因是你在model里面添加的字段 数据库 中已经存在。然后去 数据库 中删除掉对应的字段即可。xxx代表你的字段名字。 django . db . utils .Intern al Error : ( 1060 , " Dupli cat e column name 'user_id'")迁移报错解决方法 Django 是一个常用的Python Web框架,开发中可能遇到一些难以解决的异常。’gbk’ codec can’t decode byte 0xa6。原生SQL报错Unknown column 。No migration s to apply。使用DateTimeField字段提示received a naive datetime。UnorderedObjectListWarning。CentOS部署Incorrect string v al ue。持续更新中。 1.8 Django 的APP目录下有 make migration s文件夹,这个文件夹中的文件误删除过,后面从其他电脑的工程目录中拷贝了过来。 进行了python manage.py make migration s和python manage.py migrate操作。 由于前后的 make migration s的文件不一致,导致 数据库 中的字段与 Django 的Models字段不一致... django . db . utils . Operation al Error : ( 1060 , “ Dupli cat e column name ‘to_user_id’”) 在project/app/ migration s下有每次对数据操作所生成的脚本。找到出错那次脚本,打开脚本,将所对应的 数据库 更改还原(当然得使用mysql语句了)。 然后删掉这次 migration ,重新 make 然后执行。 实在不行,还有一个万... 一直出现这个问题,删除了 migration 文件里面的除了__int__.py文件继续执行,python manage.py migration python manage.py migrate --fake 这个解决问题 django . db . utils .Intern al Error : ( 1060 , “ Dupli cat e column name ‘user_id’”) 一直出现这个问题,删除了 migration 文件里面的除了__int__.py文件继续执行,python manage.py migration 如果还有问题 python manage.py migrate --fake 数据库 迁移时提示的是 django . db . utils .Intern al Error ( 1060 ,“ Dupli cat e column name‘desc_pack’”),即要寻找desc_park相关的迁移记录。出现本问题的原因在于name后的字段在之前已经进行过迁移,故而会提示已有重复列。这里不用进行什么删除操作,我们将迁移文件中的相关语句注释掉即可。相关字段注释掉,这里为防止其他列名产生相同错误我全部注释掉,因为之前已经进行过迁移操作。迁移成功后记得将注释掉的代码恢复。再次执行迁移操作,成功。... 应该是之前创建后删除之类操作导致的,查看表中字段XXX已存在 解决办法: 1, 在 django migration s目录下,在新生成的记录中,找到新建字段的语句,将其复制到前一个记录里面 2, 然后将后面(新生成文件)含有新建字段的记录删除。 之后migrate就没有报错了 同步 数据库 :使用上述两条命令 同步 数据库 1.认识 migration s目录: migration s目录作用:用来存放通过 make migration s命令生成的 数据库 脚本,里面的生成的脚本不要轻易修改。要正常的使用 数据库 同步 的功能,app目录下必须要有 migration s目录,且目录下存在__init__.py文件。2.认识一张数据表( django _ migration s)表中的字段:app:app名字na... 报错原因解释 官方说法:当查出来的虚拟表中有相同名称的字段的时候,就需要定义别名,要不无法识别, 大白话: 当执行红色框的sql语句,的出来的下面的查询结果里面,有相同名称的字段的时候,就认定为重复列名,需要指定别名,才能解决异常 针对你需要的字段,单个指定