peewee 对 mysql 类型支持问题,并不支持bit
阅读原文时间:2023年07月15日阅读:1

这个问题是当时想当然了 看到python BooleanField(default=False) 便认为 对应 mysql的bit

出现问题

mysql实际字段存储值为0,但 peewee orm 后的结果却是 True.

查官方文档

http://peewee.readthedocs.io/en/latest/peewee/models.html

Field types table

Field Type

Sqlite

Postgresql

MySQL

CharField

varchar

varchar

varchar

FixedCharField

char

char

char

TextField

text

text

longtext

DateTimeField

datetime

timestamp

datetime

IntegerField

integer

integer

integer

BooleanField

integer

boolean

bool

FloatField

real

real

real

DoubleField

real

double precision

double precision

BigIntegerField

integer

bigint

bigint

SmallIntegerField

integer

smallint

smallint

DecimalField

decimal

numeric

numeric

PrimaryKeyField

integer

serial

integer

ForeignKeyField

integer

integer

integer

DateField

date

date

date

TimeField

time

time

time

TimestampField

integer

integer

integer

BlobField

blob

bytea

blob

UUIDField

text

uuid

varchar(40)

BareField

untyped

not supported

not supported

Note

Don’t see the field you’re looking for in the above table? It’s easy to create custom field types and use them with your models.

peewee 默认不支持 mysql 的 bit类型

BooleanField 对应 mysql的bool,而这个 bool 在 mysql 里就是 tinyint

处理方式,把 mysql 的字段类型改为 bool

或自定义映射类

import uuid

class UUIDField(Field):
db_field = 'uuid'

def db\_value(self, value):  
    return str(value) # convert UUID to str

def python\_value(self, value):  
    return uuid.UUID(value) # convert str to UUID

解决