【问题标题】:OperationalError: (1101, BLOB, TEXT, GEOMETRY or JSON column field_name cant have a default value)OperationalError:(1101、BLOB、TEXT、GEOMETRY 或 JSON 列 field_name 不能有默认值)
【发布时间】:2018-11-21 21:10:52
【问题描述】:

工作环境
1. Django 1.11
2. Python 3.6
3. MySQL 社区服务器- 5.7.21

我一直在为我的 Django 模型使用 django-jsonfield

import jsonfield
from django.db import models


class MySampleModel(models.Model):
    name = models.CharField(max_length=123)
    json_data = jsonfield.JSONField(default={})


我们知道,这个特定字段不支持 JSON 查找和聚合,因为它在 MYSQL 后端创建为 LongText

所以,我决定使用 Django-MYSQL,它也支持JSONField,因此我将models.py 更改为,

import jsonfield
from django.db import models
from django_mysql.models import JSONField as MYSQLJSONField


class MySampleModel(models.Model):
    name = models.CharField(max_length=123)
    json_data = jsonfield.JSONField(default={})
    new_jsonfield = MYSQLJSONField(default=dict)


然后我运行makemigrations 命令,它成功执行并生成了迁移文件。但是,当尝试运行migrate 时,它会抛出错误,因为

django.db.utils.OperationalError: (1101, "BLOB, TEXT, GEOMETRY or JSON column 'new_jsonfield' can't have a default value")


追溯:

Running migrations:
  Rendering model states... DONE
  Applying spider.0055_run_new_jsonfield...Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 112, in execute
    return self.cursor.execute(query, args)
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/cursors.py", line 226, in execute
    self.errorhandler(self, exc, value)
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorvalue
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/cursors.py", line 217, in execute
    res = self._query(query)
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/cursors.py", line 378, in _query
    rowcount = self._do_query(q)
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/cursors.py", line 341, in _do_query
    db.query(q)
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 280, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1101, "BLOB, TEXT, GEOMETRY or JSON column 'new_jsonfield' can't have a default value")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "./manage.py", line 8, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
    field,
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/schema.py", line 50, in add_field
    super(DatabaseSchemaEditor, self).add_field(model, field)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 396, in add_field
    self.execute(sql, params)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 110, in execute
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python3.6/site-packages/raven/contrib/django/client.py", line 123, in execute
    return real_execute(self, sql, params)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 112, in execute
    return self.cursor.execute(query, args)
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/cursors.py", line 226, in execute
    self.errorhandler(self, exc, value)
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorvalue
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/cursors.py", line 217, in execute
    res = self._query(query)
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/cursors.py", line 378, in _query
    rowcount = self._do_query(q)
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/cursors.py", line 341, in _do_query
    db.query(q)
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 280, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1101, "BLOB, TEXT, GEOMETRY or JSON column 'new_jsonfield' can't have a default value")


生成的迁移文件

from __future__ import unicode_literals

from django.db import migrations
import django_mysql.models


class Migration(migrations.Migration):

    dependencies = [
        ('app_name', 'some_prev_migrationfile'),
    ]

    operations = [
        migrations.AddField(
            model_name='mysamplemodel',
            name='new_jsonfield',
            field=django_mysql.models.JSONField(default=dict),
        ),
    ]


问题

在这种情况下,我如何安全地django-jsonfield 迁移到 Django-MYSQL

【问题讨论】:

  • 您的错误看起来与this issue 非常相似,尽管该票证上的 cmets 建议它应该在 Django 1.11 中修复。

标签: python mysql django django-models django-mysql


【解决方案1】:

这应该可行-

import jsonfield
from django.db import models
from django_mysql.models import JSONField as MYSQLJSONField


class MySampleModel(models.Model):
    name = models.CharField(max_length=123)
    json_data = jsonfield.JSONField(default={})
    new_jsonfield = MYSQLJSONField(default={})

或者试试:

import jsonfield
from django.db import models
from django_mysql.models import JSONField as MYSQLJSONField

def default_json():
    return {'data': 'bar'}

class MySampleModel(models.Model):
    name = models.CharField(max_length=123)
    json_data = jsonfield.JSONField(default={})
    new_jsonfield = MYSQLJSONField(default=default_json)

希望这会有所帮助:)

【讨论】:

  • 得到错误,(django_mysql.E017) 不要对 JSONField 使用可变默认值 提示:可变默认值在字段的所有实例之间共享,这可能不是您想要的。你应该用一个可调用的替换你的默认值,例如将 default={} 替换为 default=dict。您传递的默认值为 '{}'
  • json_data = jsonfield.JSONField(default={}),我认为这对你有用。你不是对两列都使用相同的吗??
  • 我尝试为default 拨打callable function。但是,它显示的错误与我在 Question 中提到的相同
  • 正如我在 OP 中所说,jsonfield 不支持 json-lookups
猜你喜欢
  • 2016-11-20
  • 1970-01-01
  • 1970-01-01
  • 2018-04-02
  • 2015-05-22
  • 2013-03-01
  • 1970-01-01
  • 2021-01-17
  • 2015-03-16
相关资源
最近更新 更多