对于新建的一个项目,新建的app,在数据库迁移的时候,执行了下面两条命令:
python manage.py makemigrations teacher_app,第一步成功
python manage.py migrate teacher_app,第二步失败,报错django.db.utils.OperationalError: no such table: django_content_type
解决方法:
python manage.py makemigrations teacher_app
python manage.py migrate
原因:
-
migrate
,负责对INSTALLED_APPS中的应用程序迁移。 -
makemigrations
, 负责基于你的模型修改创建一个新的迁移 -
sqlmigrate
, 展示迁移的sql语句 -
showmigrations
,其中列出了项目的迁移及其状态。
Python manger.py makemigrations:会在当前目录下生成一个migrations文件夹,该文件夹的内容就是数据库要执行的内容,会生成sql语句,可以使用python manger.py sqlmigrate app 0001查看具体的sql语句。
python manager.py migrate:执行之前生成的migrations文件,将该动作作用到数据库文件,生成表。
python manage.py migrate app_name:只执行app_name这个应用下的migrations文件,生成对应的表,对其它的migrations文件不执行。但是在创建一个新的项目后,会为我们自动生成一些migrations文件,只有在这些默认的migrations文件执行了,生成默认提供的表以后(如:django_content_type,auth_permission等),才能执行我们自己写的migrations文件,生成我们自己想要的表。
问题解决具体过程。
我的环境:Centos6.8+anaconda+pycharm+python3.7+Django1.8
这是我的项目建立过程:
1.建立项目
(Django) [[email protected] Django]# django-admin startproject teacher
(Django) [[email protected] Django]# cd teacher
(Django) [[email protected] teacher]# python manage.py startapp teacher_app
2.编写类,在teacher_app的models.py文件中
from django.db import models
# Create your models here.
class Teacher(models.Model):
name = models.CharField(max_length=20)
age = models.IntegerField()
address = models.CharField(max_length=20)
email = models.CharField(max_length=20)
def __str__(self):
return self.name
3.添加app到setting
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'teacher_app',
)
4.进行数据库迁移:
(Django) [[email protected] teacher]# python manage.py makemigrations teacher_app
Migrations for 'teacher_app':
0001_initial.py:
- Create model Teacher
(Django) [[email protected] teacher]# python manage.py migrate teacher_app
第二步失败。报错信息:Django.db.utils.OperationalError: no such table: django_content_type、
"Error creating new content types. Please make sure contenttypes "
RuntimeError: Error creating new content types. Please make sure contenttypes is migrated before trying to migrate apps individually.
具体报错信息:
(Django) [[email protected] teacher]# python manage.py makemigrations teacher_app
Migrations for 'teacher_app':
0001_initial.py:
- Create model Teacher
(Django) [[email protected] teacher]# python manage.py migrate teacher_app
Operations to perform:
Apply all migrations: teacher_app
Running migrations:
Rendering model states... DONE
Applying teacher_app.0001_initial... OK
Traceback (most recent call last):
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 318, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: django_content_type
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/contrib/contenttypes/models.py", line 65, in get_for_model
ct = self.get(app_label=opts.app_label, model=opts.model_name)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/models/manager.py", line 127, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/models/query.py", line 328, in get
num = len(clone)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/models/query.py", line 144, in __len__
self._fetch_all()
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/models/query.py", line 965, in _fetch_all
self._result_cache = list(self.iterator())
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/models/query.py", line 238, in iterator
results = compiler.execute_sql()
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 829, in execute_sql
cursor.execute(sql, params)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/utils/six.py", line 658, in reraise
raise value.with_traceback(tb)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 318, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: django_content_type
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 225, in handle
emit_post_migrate_signal(created_models, self.verbosity, self.interactive, connection.alias)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/core/management/sql.py", line 280, in emit_post_migrate_signal
using=db)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 201, in send
response = receiver(signal=self, sender=sender, **named)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/contrib/auth/management/__init__.py", line 82, in create_permissions
ctype = ContentType.objects.db_manager(using).get_for_model(klass)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/contrib/contenttypes/models.py", line 78, in get_for_model
"Error creating new content types. Please make sure contenttypes "
RuntimeError: Error creating new content types. Please make sure contenttypes is migrated before trying to migrate apps individually.
错误信息告诉我在进行数据库迁移的时候应确保contenttypes被迁移了。接着我就开始迁移contenttypes,但还是报错,错误是“django.db.utils.OperationalError: no such table: auth_permission”,具体如下:
(Django) [[email protected] teacher]# python manage.py migrate contenttypes
Operations to perform:
Apply all migrations: contenttypes
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Traceback (most recent call last):
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 318, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: auth_permission
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 225, in handle
emit_post_migrate_signal(created_models, self.verbosity, self.interactive, connection.alias)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/core/management/sql.py", line 280, in emit_post_migrate_signal
using=db)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 201, in send
response = receiver(signal=self, sender=sender, **named)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/contrib/auth/management/__init__.py", line 93, in create_permissions
"content_type", "codename"
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/models/query.py", line 162, in __iter__
self._fetch_all()
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/models/query.py", line 965, in _fetch_all
self._result_cache = list(self.iterator())
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/models/query.py", line 1220, in iterator
for row in compiler.results_iter():
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 783, in results_iter
results = self.execute_sql(MULTI)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 829, in execute_sql
cursor.execute(sql, params)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/utils/six.py", line 658, in reraise
raise value.with_traceback(tb)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/root/anaconda3/envs/Django/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 318, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: auth_permission
后来我查资料,知道了类似django_content_type 、auth_permission等这样的表是默认创建的,刚开始以为是我的Django版本问题,但是之前照着视频学习的时候,运行没问题的。看来还是我的之前操作不到位。
我继续调试,输入命令:python manage.py showmigrations,这个命令是显示出Django项目中的所有migrations文件及其状态,
[x] 代表已经执行完毕的migrations文件, []表示未执行或执行失败的文件。
原来我是没有执行这些为我们默认配置的migrations。接下来我继续执行命令:python manage.py migrate ,这次没有带上应用名。
可以了,执行成功了,再检验一下,执行python manage.py showmigrations,所有的migrations都被执行了。
我再进一步确定数据库好不好用,查询数据,显示没问题。