我将我的Django项目连接到MySQL数据库,我确信它实际上是连接的,因为当我尝试制作数据时。我检查了MySQL工作台,我的所有模型都被同步到数据库中。但是,问题是当我尝试迁移时,我收到此错误。由于mysql.connector.errors.IntegrityError:1048(23000):Column' content_type_id'是抱怨没有要应用的迁移。不能为空。我甚至不确定content_type_id来自哪里,因为我的模型甚至没有。
Operations to perform:
Target specific migration: 0001_initial, from bookSell
Running migrations:
No migrations to apply.
Traceback (most recent call last):
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/mysql/connector/django/base.py", line 177, in _execute_wrapper
return method(query, args)
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/mysql/connector/cursor.py", line 515, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/mysql/connector/connection.py", line 488, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/mysql/connector/connection.py", line 395, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.IntegrityError: 1048 (23000): Column 'content_type_id' cannot be null
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 "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 224, in handle
self.verbosity, self.interactive, connection.alias, apps=post_migrate_apps, plan=plan,
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/django/core/management/sql.py", line 53, in emit_post_migrate_signal
**kwargs
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/django/dispatch/dispatcher.py", line 191, in send
response = receiver(signal=self, sender=sender, **named)
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/django/contrib/auth/management/__init__.py", line 83, in create_permissions
Permission.objects.using(using).bulk_create(perms)
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/django/db/models/query.py", line 452, in bulk_create
ids = self._batched_insert(objs_without_pk, fields, batch_size)
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/django/db/models/query.py", line 1068, in _batched_insert
self._insert(item, fields=fields, using=self.db)
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/django/db/models/query.py", line 1045, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1054, in execute_sql
cursor.execute(sql, params)
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/mysql/connector/django/base.py", line 227, in execute
return self._execute_wrapper(self.cursor.execute, query, new_args)
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/mysql/connector/django/base.py", line 183, in _execute_wrapper
utils.IntegrityError(err.msg), sys.exc_info()[2])
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/mysql/connector/django/base.py", line 177, in _execute_wrapper
return method(query, args)
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/mysql/connector/cursor.py", line 515, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/mysql/connector/connection.py", line 488, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/Users/JenniferLiu/.virtualenvs/bookSell/lib/python3.5/site-packages/mysql/connector/connection.py", line 395, in _handle_result
raise errors.get_exception(packet)
django.db.utils.IntegrityError: Column 'content_type_id' cannot be null
这是我的model.py:
class Book(models.Model):
title = models.CharField(max_length=200,default='')
author = models.CharField(max_length=200,default='')
year_published = models.DateField(default='1998-09-18')
description = models.CharField(max_length = 500,default='')
rating = models.IntegerField(default=1)
这是迁移0001_initial.py:
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Book',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(default='', max_length=200)),
('author', models.CharField(default='', max_length=200)),
('year_published', models.DateField(default='1998-09-18')),
('description', models.CharField(default='', max_length=500)),
('rating', models.IntegerField(default=1)),
],
),
]
python manage.py makemigrations运行正常。这是结果
Migrations for 'bookSell':
bookSell/migrations/0001_initial.py:
- Create model Book
非常感谢您的帮助,谢谢您,如果需要任何其他信息来解决此问题,请发表评论。 :d
编辑2 Setting.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'bookSell',
]
所以我设法将python运行到runserver,而在admin中我确实让它与Books Model一起运行,但问题是当我尝试添加书籍时。我收到了错误,列&#39; user_id&#39;不能为空