我将 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_packet'
更新为 1073741824
但这并没有改变任何东西。
顺便说一下,我认为这是同样的问题,将 long_str
与 PickledField
一起使用会给我一个损坏的管道错误。
有没有办法告诉 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/