Django报错django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency users.0001_initial on database 'default'.
1、出现原因,今天重写django的user模型,执行python manage.py makemigrations没报错,后面继续执行python manage.py migrate确报出了这个错,完全日志:
(venv) [root@localhost drf_ops]# python manage.py migrate users
(0.000)
SELECT VERSION(),
@@sql_mode,
@@default_storage_engine,
@@sql_auto_is_null,
@@lower_case_table_names,
CONVERT_TZ('2001-01-01 01:00:00', 'UTC', 'UTC') IS NOT NULL
; args=None
(0.000) SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; args=None
(0.001)
SELECT VERSION(),
@@sql_mode,
@@default_storage_engine,
@@sql_auto_is_null,
@@lower_case_table_names,
CONVERT_TZ('2001-01-01 01:00:00', 'UTC', 'UTC') IS NOT NULL
; args=None
(0.000) SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; args=None
(0.001) SHOW FULL TABLES; args=None
(0.002) SELECT `django_migrations`.`id`, `django_migrations`.`app`, `django_migrations`.`name`, `django_migrations`.`applied` FROM `django_migrations`; args=()
(0.001) SHOW FULL TABLES; args=None
(0.001) SELECT `django_migrations`.`id`, `django_migrations`.`app`, `django_migrations`.`name`, `django_migrations`.`applied` FROM `django_migrations`; args=()
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/srv/drf_ops/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/srv/drf_ops/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/srv/drf_ops/venv/lib/python3.7/site-packages/django/core/management/base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "/srv/drf_ops/venv/lib/python3.7/site-packages/django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
File "/srv/drf_ops/venv/lib/python3.7/site-packages/django/core/management/base.py", line 89, in wrapped
res = handle_func(*args, **kwargs)
File "/srv/drf_ops/venv/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 95, in handle
executor.loader.check_consistent_history(connection)
File "/srv/drf_ops/venv/lib/python3.7/site-packages/django/db/migrations/loader.py", line 310, in check_consistent_history
connection.alias,
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency users.0001_initial on database 'default'.
(venv) [root@localhost drf_ops]#
2、user模型定义如下:
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class UserProfile(AbstractUser):
phone = models.CharField(max_length=11, null=True, blank=True, verbose_name='手机号')
avatar = models.CharField(max_length=100, null=True, blank=True, verbose_name='头像')
class Meta:
db_table = 'auth_user'
verbose_name = "用户信息"
verbose_name_plural = verbose_name
def __str__(self):
return self.username
3、解决建议,如果数据库中数据不是太多可以备份后删掉所有表重新生成即可解决,下面是本次解决过程
1、定义好模型可以配置setting.py后操作,执行命令:python manage.py makemigrations users
(venv) [root@localhost drf_ops]# python manage.py makemigrations users
(0.000)
SELECT VERSION(),
@@sql_mode,
@@default_storage_engine,
@@sql_auto_is_null,
@@lower_case_table_names,
CONVERT_TZ('2001-01-01 01:00:00', 'UTC', 'UTC') IS NOT NULL
; args=None
(0.000) SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; args=None
(0.001) SHOW FULL TABLES; args=None
(0.002) SELECT `django_migrations`.`id`, `django_migrations`.`app`, `django_migrations`.`name`, `django_migrations`.`applied` FROM `django_migrations`; args=()
Migrations for 'users':
apps/users/migrations/0001_initial.py
- Create model UserProfile
(venv) [root@localhost drf_ops]#
2、手动执行SQL插入表记录,APP名称和记录文件对应上面生成的
INSERT INTO django_migrations (app, name, applied) VALUES ('users', '0001_initial', CURRENT_TIMESTAMP);
3、接着继续执行python manage.py migrate users 如果报 not installed users.userprofile就继续手动执行下面的SQL插入表记录,接着在继续操作migrate即可
INSERT INTO django_content_type(app_label, model) VALUES ('users', 'userprofile');
4、检查auth_user表是否生成了新加的字段,如果没有就手动添加上即可
ps: 用回原来的表名auth_user才可能不会生成新加的字段