python - Peewee CompressedField 在 MySQL 数据库上被截断

标签 python mysql peewee pymysql

我将 Peewee 与 PyMySQL 一起使用,在尝试使用 playhouse 中的 CompressedField 时,我遇到了 64k blob 大小问题> 模块...

以下代码为我提供了第二次测试的截断数据

from peewee import *
from playhouse.db_url import connect
from playhouse.fields import CompressedField

db = connect("mysql://me:pass@IP/test_db")

class Compress(Model):
    name = CharField()
    cmprssd_data = CompressedField()

    class Meta:
        database = db

db.connect()
db.create_tables([Compress], safe=True)

short_str = "".zfill(200)
di_test1 = {"name": "first", "cmprssd_data": short_str }
test1 = Compress(**di_test1)
test1.save()

long_str = "".zfill(200000000)
di_test2 = {"name": "second", "cmprssd_data": long_str }
test2 = Compress(**di_test2)
test2.save()

我尝试在 MySQL 和 pymysql 中将 'max_allowed_pa​​cket' 更新为 1073741824 但这并没有改变任何东西。 顺便说一下,我认为这是同样的问题,将 long_strPickledField 一起使用会给我一个损坏的管道错误。

有没有办法告诉 peewee 使用 longblob? (或者问题是否来自其他地方?) 我还找到了一个 thread on the broken pipe problem with pymysql但我不知道如何告诉 Peewee 模型在那个特定领域做大块的事情......

最佳答案

这可以通过 custom field 来完成:

from peewee import *
from playhouse.fields import CompressedField

# tell database that the longblob column type exists
db = MySQLDatabase('test', host='127.0.0.1', user='root',
                   fields={'longblob': 'longblob'})


# create a custom field, in this case subclassing the existing field
class LongCompressedField(CompressedField):
    db_field = 'longblob'  # <-- this matches our dictionary key above


class MyModel(Model):
    val = LongCompressedField()

    class Meta:
        db_table = 'test'
        database = db


db.connect()
db.create_table(MyModel, safe=True)

m = MyModel(val=''.zfill(200000000))
m.save()

关于python - Peewee CompressedField 在 MySQL 数据库上被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36205416/

相关文章:

PHP变量正确回显但未进入数据库

mysql - scrapy 和 mysql

python - 如何将多个条件传递给 python peewee 的 join 参数?

python - 我如何让 Mypy 意识到 isinstance 已经被调用了?

python - random.choice() 接受 2 个位置参数,但给出了 3 个

python - np.mean 的 Numba 失败

PHP使用mysql在变量中发布变量

python - 在多对多关系中搜索项目

python - Peewee 模型中的 getter 和 setter 方法

python - 使用 pandas,从长格式 df 中提取数据并将其添加到宽格式 df