【问题标题】:Peewee CompressedField gets truncated on MySQL dbPeewee CompressedField 在 MySQL db 上被截断
【发布时间】:2016-07-12 08:27:09
【问题描述】:

我在 PyMySQL 中使用 Peewee,但在尝试使用 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_strPickledField 会给我一个损坏的管道错误。

有没有办法告诉 peewee 与 longblob 合作? (或者问题是否来自其他地方?) 我还找到了thread on the broken pipe problem with pymysql,但我不知道如何告诉 Peewee 模型在该特定字段上执行块内容...

【问题讨论】:

    标签: python mysql peewee pymysql


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      • 2014-03-05
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      相关资源
      最近更新 更多