Django 迁移:django.db.utils.OperationalError:(1364,“字段'name'没有默认值”)

2024-09-28 12:13:34 发布

您现在位置:Python中文网/ 问答频道 /正文

基本数据:

mysql-server: 5.7.27-0ubuntu0.16.04.1
Ubuntu: 16.04
Python: 2.7.12
Django: 1.11.16
Virtualenv

我添加了一个新的应用程序(gallery),并创建了下表:

^{pr2}$

在得到上述错误之后,为了保持整洁,我从数据库中删除了表、django_migrations表中的条目以及所有迁移文件,但是__init__.py。在

我运行了makemigrations,之后{},其输出如下:

# 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配置文件中删除严格的\u TRANS_表之后,这个错误变成了一个警告,但仍然很烦人。是什么原因造成的?在


Tags: djangoinpyselfhomedblibpackages
1条回答
网友
1楼 · 发布于 2024-09-28 12:13:34

在最新版本中,MySQL强制要求为NOT NULL列添加默认值。它是由STRICT_TRANS_TABLES模式引起的。有三种方法可以解决这个问题

1)向not null列添加默认值。在

    ALTER TABLE tablename MODIFY columname DATATYPE NOT NULL DEFAULT 'a value'

2)默认为空值

^{pr2}$

3)在MySQL配置文件中,从sql_mode中删除{},然后重新启动服务。在

相关问题 更多 >

    热门问题