Cannot store BLOB fields with peewee and PyMySQL
See original GitHub issueHi,
I have the problem, that I cannot store Models having a BlobField with binary content since upgrading to the latest version of peewee 5aea8f6fa405c896884c71262e0b2ac04d8cdaf9. I am using a MySQL database with peewee over PyMySQL b2ec8287151f8dc54e71e426b41a1b300fb934bb.
How to reproduce:
- Install peewee and PyMySQL versions as mentioned above
- Create a MySQL databse with a table containing a BLOB field
- Create an appropriate peewee.Model
- Create a new Model instance
- Set the BlobField to a non-text binary (e.g. a picture file)
- Try to store the model with model.save()
- Get UnicodeEncodeError exceptions like:
'utf-8' codec can't encode character '\udcff' in position 159: surrogates not allowed in <class 'homie.mods.openimmodb.openimmodb.Anhaenge'>
'utf-8' codec can't encode character '\udcff' in position 160: surrogates not allowed in <class 'homie.mods.openimmodb.openimmodb.Anhaenge'>
'utf-8' codec can't encode character '\udcff' in position 161: surrogates not allowed in <class 'homie.mods.openimmodb.openimmodb.Anhaenge'>
'utf-8' codec can't encode character '\udc89' in position 163: surrogates not allowed in <class 'homie.mods.openimmodb.openimmodb.Anhaenge'>
'utf-8' codec can't encode character '\udcc7' in position 185: surrogates not allowed in <class 'homie.mods.openimmodb.openimmodb.Anhaenge'>
Thanks for looking into it.
PS: peewee does not seem compatible with the surrogates escapes introduced in PyMySQL 5157c9b7d058f764695a35684b599e0cbac22d9c : https://github.com/PyMySQL/PyMySQL/commit/5157c9b7d058f764695a35684b599e0cbac22d9c
Since this was a fix in PyMySQL, I assume, that peewee needs to correct for compatibility here, or am I wrong?
Issue Analytics
- State:
- Created 9 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Broken Pipe when I try to insert large blob with pymysql
Files are stored in a mysql database. This works fine until the file is larger than around 16Mb, the insert fails with the...
Read more >Database — peewee 3.15.4 documentation
Database¶. The Peewee Database object represents a connection to a database. The Database class is instantiated with all the information needed to open...
Read more >Peewee - Quick Guide - Tutorialspoint
Binary fields. The binary fields in Peewee are explained below − ... However, unlike SQLite database, Peewee can't create a MySql database.
Read more >peewee — Debian bullseye
As another example, the pymysql driver accepts a charset parameter which is not a standard Peewee Database parameter. To set this value, ...
Read more >Unicode issues while using peewee, pymysql, and python
So I copied your code into a script file, created a local database and set up the proper fields. I threw some random...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think the best path forward will be to subclass
BlobField
in your app and use custom logic for thedb_value()
function:I think that peewee handles this datatype correctly. According to PEP 249, the db-api 2.0 spec:
When using a
BlobField
, peewee usesbuffer
in Python2 andbytes
for Python3.