django 数据库migrate出错

来源:3-7 xadmin后台管理系统的配置

Y2sh

2019-03-18

Applying trade.0002_auto_20190318_2058…Traceback (most recent call last):
File “D:\Envs\dianshan\lib\site-packages\django\db\backends\utils.py”, line 85, in _execute
return self.cursor.execute(sql, params)
File “D:\Envs\dianshan\lib\site-packages\django\db\backends\mysql\base.py”, line 71, in execute
return self.cursor.execute(query, args)
File “D:\Envs\dianshan\lib\site-packages\MySQLdb\cursors.py”, line 206, in execute
res = self._query(query)
File “D:\Envs\dianshan\lib\site-packages\MySQLdb\cursors.py”, line 312, in _query
db.query(q)
File “D:\Envs\dianshan\lib\site-packages\MySQLdb\connections.py”, line 224, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.OperationalError: (1060, “Duplicate column name ‘user_id’”)

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

Traceback (most recent call last):
File “D:\pycharm\PyCharm 2018.3.3\helpers\pycharm\django_manage.py”, line 52, in
run_command()
File “D:\pycharm\PyCharm 2018.3.3\helpers\pycharm\django_manage.py”, line 46, in run_command
run_module(manage_file, None, ‘main’, True)
File “c:\users\jia jun\appdata\local\programs\python\python37\Lib\runpy.py”, line 205, in run_module
return _run_module_code(code, init_globals, run_name, mod_spec)
File “c:\users\jia jun\appdata\local\programs\python\python37\Lib\runpy.py”, line 96, in run_module_code
mod_name, mod_spec, pkg_name, script_name)
File “c:\users\jia jun\appdata\local\programs\python\python37\Lib\runpy.py”, line 85, in run_code
exec(code, run_globals)
File “D:/program/dianshan1\manage.py”, line 15, in
execute_from_command_line(sys.argv)
File "D:\Envs\dianshan\lib\site-packages\django\core\management_init
.py", line 381, in execute_from_command_line
utility.execute()
File "D:\Envs\dianshan\lib\site-packages\django\core\management_init
.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File “D:\Envs\dianshan\lib\site-packages\django\core\management\base.py”, line 316, in run_from_argv
self.execute(*args, **cmd_options)
File “D:\Envs\dianshan\lib\site-packages\django\core\management\base.py”, line 353, in execute
output = self.handle(*args, **options)
File “D:\Envs\dianshan\lib\site-packages\django\core\management\base.py”, line 83, in wrapped
res = handle_func(*args, **kwargs)
File “D:\Envs\dianshan\lib\site-packages\django\core\management\commands\migrate.py”, line 203, in handle
fake_initial=fake_initial,
File “D:\Envs\dianshan\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 “D:\Envs\dianshan\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 “D:\Envs\dianshan\lib\site-packages\django\db\migrations\executor.py”, line 244, in apply_migration
state = migration.apply(state, schema_editor)
File “D:\Envs\dianshan\lib\site-packages\django\db\migrations\migration.py”, line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File “D:\Envs\dianshan\lib\site-packages\django\db\migrations\operations\fields.py”, line 84, in database_forwards
field,
File “D:\Envs\dianshan\lib\site-packages\django\db\backends\mysql\schema.py”, line 42, in add_field
super().add_field(model, field)
File “D:\Envs\dianshan\lib\site-packages\django\db\backends\base\schema.py”, line 435, in add_field
self.execute(sql, params)
File “D:\Envs\dianshan\lib\site-packages\django\db\backends\base\schema.py”, line 133, in execute
cursor.execute(sql, params)
File “D:\Envs\dianshan\lib\site-packages\django\db\backends\utils.py”, line 100, in execute
return super().execute(sql, params)
File “D:\Envs\dianshan\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 “D:\Envs\dianshan\lib\site-packages\django\db\backends\utils.py”, line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File “D:\Envs\dianshan\lib\site-packages\django\db\backends\utils.py”, line 85, in _execute
return self.cursor.execute(sql, params)
File “D:\Envs\dianshan\lib\site-packages\django\db\utils.py”, line 89, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File “D:\Envs\dianshan\lib\site-packages\django\db\backends\utils.py”, line 85, in _execute
return self.cursor.execute(sql, params)
File “D:\Envs\dianshan\lib\site-packages\django\db\backends\mysql\base.py”, line 71, in execute
return self.cursor.execute(query, args)
File “D:\Envs\dianshan\lib\site-packages\MySQLdb\cursors.py”, line 206, in execute
res = self._query(query)
File “D:\Envs\dianshan\lib\site-packages\MySQLdb\cursors.py”, line 312, in _query
db.query(q)
File “D:\Envs\dianshan\lib\site-packages\MySQLdb\connections.py”, line 224, in query
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (1060, “Duplicate column name ‘user_id’”)

已经试过把migrations 里除了__init__.py外的文件删掉后重新makemigrations,再migrate还是报错

写回答

1回答

uipo

2019-03-18

把数据库一起删了,再makemigrations.这个我也碰到过

0
2
bobby
回复
Y2sh
django.db.utils.OperationalError: (1060, “Duplicate column name ‘user_id’”) 这个报错的意思是你的一个model中定了相同的列,比如你在同一个model中定定义了两个列都是user你检查一下是哪个model这个列定义重复了
2019-03-19
共2条回复

Python前后端分离开发Vue+Django REST framework实战

Django REST framework课程视频,RESTFul API前后端分离开发

2873 学习 · 2457 问题

查看课程