2016-03-24 106 views
2

我使用Peewee與PyMySQL,並試圖從playhouse模塊使用CompressedField當我被陷在64K blob大小...Peewee CompressedField被截斷的MySQL數據庫

下面的代碼給我第二次測試

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

db = connect("mysql://me:[email protected]/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() 

截斷的數據我想更新'max_allowed_packet'在兩個MySQL的1073741824和pymysql但這並沒有改變任何東西。 順便說一句,因爲我認爲這是同樣的問題,使用long_strPickledField給我一個壞的管道錯誤。

有沒有告訴peewee與longblob一起工作的方法? (或問題來自其他地方?) 我也發現了一個thread on the broken pipe problem with pymysql,但我不知道如何告訴一個Peewee模型做該塊特定領域的塊...

回答

0

這可以用a 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() 
+0

這個效果非常好,非常感謝! – Silmathoron