MySQL存在操作错误,SQLite3没有

时间:2017-07-18 22:04:11

标签: python mysql django

我正在使用SQLite3构建我的Django网站,现在我正在将其转移到生产中,我想转储数据库并切换到MySQL。

但是,当我在生产服务器上运行python manage.py migrate时,发生以下错误:

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

travelers指的是以下model定义:

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

我不明白这是如何在SQLite3数据库上迁移得很好但是现在我遇到了MySQL问题。我做错了吗?

1 个答案:

答案 0 :(得分:1)

我的猜测是问题是使用None作为不允许NULL值的字段的默认值。如果您将travelersdates字段定义更改为

,它应该有效
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)

(两者都添加null=True