简体   繁体   中英

Django migrate: django.db.utils.OperationalError: (1364, "Field 'name' doesn't have a default value")

basic datas:

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

I added a new application (gallery) and created the following table:

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)

To be very clean after getting the above mentioned errors I deleted the table from the database, the entries from the django_migrations table and all the migrations file but __init__.py .

I ran makemigrations and after that migrate which had the following output:

# 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")

What is strange is that even if there's the error it creates the table. I can save objects there and there's also an entry in the django_migrations table. Also, showmigrations shows that this migrations is done. However, when I run the migrate command again I get the same error.

My question is what can cause this problem and how can I solve it? After removing the STRICT_TRANS_TABLES from the mysql config file, this error turns to be a warning, but it's still annoying. What causes this?

MySQL in latest versions has made it mandatory to add default value for NOT NULL columns. It is caused due to STRICT_TRANS_TABLES mode. There are 3 ways to fix it,

1) Add default value to not null columns.

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

2) Set NULL values by default

    ALTER TABLE tablename MODIFY columname DATATYPE NULL

3) In MySQL configuration file, remove STRICT_TRANS_TABLES from sql_mode and restart the service.

I did not need table. I deleted table then ran migrations and migrate again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM