DDiran
DDiran

Reputation: 573

MySQL has operational error where SQLite3 does not

I was building my Django website with SQLite3 and now that I am moving it to production I would like to dump the database and switch to MySQL.

As I ran python manage.py migrate on the production server however, the following error occured:

Traceback (most recent call last):
  File "manage.py", line 12, in <module>
    execute_from_command_line(sys.argv)
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/django/db/migrations/executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 215, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 514, in alter_field
    old_db_params, new_db_params, strict)
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 683, in _alter_field
    params,
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 120, in execute
    cursor.execute(sql, params)
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 101, in execute
    return self.cursor.execute(query, args)
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/MySQLdb/cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/MySQLdb/cursors.py", line 247, in execute
    res = self._query(query)
  File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/MySQLdb/cursors.py", line 411, in _query
    rowcount = self._do_query(q)
File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/MySQLdb/cursors.py", line 374, in _do_query
    db.query(q)
      File "/home/my-project/.virtualenvs/my-project/lib/python3.5/site-packages/MySQLdb/connections.py", line 292, in query
        _mysql.connection.query(self, query)
    django.db.utils.OperationalError: (1067, "Invalid default value for 'travelers'")

Where travelers refers to the following model definition:

class ContactRequest(models.Model):
    DATE_CHOICES = (
        (None, 'When are you traveling?'),
        ('Within the next 3 months', 'Within the next 3 months'),
        # etc etc etc
    )

    NO_TRAVELERS = (
        (None, 'How many travelers?'),
        ('One', '1'),
        # etc etc etc
    )

    name = models.CharField(max_length=100)
    email = models.EmailField(max_length=254)
    travelers = models.CharField(max_length=100, choices=NO_TRAVELERS, blank=False, default=None)
    dates = models.CharField(max_length=100, choices=DATE_CHOICES, blank=False, default=None)
    message = models.TextField(max_length=2000)

    def __str__(self):
        return self.email

What I don't understand is how this migrated just fine on the SQLite3 database however now I am running into issues with the MySQL one. Am I doing something wrong?

Upvotes: 0

Views: 135

Answers (1)

kristaps
kristaps

Reputation: 1723

My guess is that the problem is using None as the default value for a field which doesn't allow NULL values. It should work if you change the travelers and dates field definitions to

travelers = models.CharField(max_length=100, choices=NO_TRAVELERS, blank=False, null=True, default=None)
dates = models.CharField(max_length=100, choices=DATE_CHOICES, blank=False, null=True, default=None)

(Added null=True to both)

Upvotes: 1

Related Questions