基本数据:
mysql-server: 5.7.27-0ubuntu0.16.04.1
Ubuntu: 16.04
Python: 2.7.12
Django: 1.11.16
Virtualenv
我添加了一个新的应用程序(图库)并创建了下表:
class Medi(models.Model):
gd_id = models.CharField(max_length=128)
name = models.CharField(max_length=512, default='1')
original_name = models.CharField(max_length=512)
title = models.CharField(max_length=512, blank=True, null=True)
md5sum = models.CharField(max_length=32)
date_of_event = models.DateTimeField()
date_of_event_end = models.DateTimeField(blank=True, null=True)
last_updated = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
为得到上述错误,我非常干净,我从数据库中删除了该表,删除了django_migrations表中的条目以及除__init__.py
以外的所有迁移文件。
我跑了makemigrations
,然后跑了migrate
,它的输出如下:
# python manage.py makemigrations
Migrations for 'gallery':
gallery/migrations/0001_initial.py
- Create model Medi
# python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, bugtrack, common, contenttypes, follow, gallery, infokom, phonebook, sessions, sites
Running migrations:
Applying gallery.0001_initial... OK
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 227, in handle
self.verbosity, self.interactive, connection.alias, apps=post_migrate_apps, plan=plan,
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/core/management/sql.py", line 53, in emit_post_migrate_signal
**kwargs
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 193, in send
for receiver in self._live_receivers(sender)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 63, in create_permissions
ctype = ContentType.objects.db_manager(using).get_for_model(klass)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 60, in get_for_model
model=opts.model_name,
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/query.py", line 466, in get_or_create
return self._create_object_from_params(lookup, params)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/query.py", line 500, in _create_object_from_params
obj = self.create(**params)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/query.py", line 394, in create
obj.save(force_insert=True, using=self.db)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/base.py", line 808, in save
force_update=force_update, update_fields=update_fields)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/base.py", line 838, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/base.py", line 924, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/base.py", line 963, in _do_insert
using=using, raw=raw)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/query.py", line 1079, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1112, in execute_sql
cursor.execute(sql, params)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 101, in execute
return self.cursor.execute(query, args)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/home/lenovo/.virtualenvs/gjt/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.OperationalError: (1364, "Field 'name' doesn't have a default value")
奇怪的是,即使有错误,它也会创建表。我可以在那里保存对象,并且django_migrations
表中还有一个条目。同样,showmigrations
显示此迁移已完成。但是,当我再次运行migrate
命令时,也会遇到相同的错误。
我的问题是什么会导致此问题,我该如何解决?从mysql配置文件中删除 STRICT_TRANS_TABLES 后,此错误将显示为警告,但仍很烦人。是什么原因造成的?
答案 0 :(得分:1)
MySQL
已强制为NOT NULL
列添加默认值。这是由于STRICT_TRANS_TABLES
模式引起的。有3种解决方法,
1)将默认值添加到非空列。
ALTER TABLE tablename MODIFY columname DATATYPE NOT NULL DEFAULT 'a value'
2)默认设置为空值
ALTER TABLE tablename MODIFY columname DATATYPE NULL
3)在MySQL
配置文件中,从STRICT_TRANS_TABLES
中删除sql_mode
并重新启动服务。