Peewee CompressedField在MySQL db上被截断

时间:2016-03-24 16:37:07

标签: python mysql peewee pymysql

我正在使用Peewee和PyMySQL,并且当我尝试使用blob模块中的CompressedField时,我被困在64k playhouse尺寸上...

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

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_packet'更新为1073741824,但这并没有改变任何内容。 顺便说一句,正如我认为同样的问题,使用long_strPickledField会给我一个管道错误。

有没有办法让peewee与longblob合作? (或问题来自其他地方?) 我还找到了thread on the broken pipe problem with pymysql,但我不知道怎么告诉Peewee模型在那个特定的领域做大块的东西......

1 个答案:

答案 0 :(得分:0)

这可以通过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()