[英]SQLAlchemy TypeDecorator for BigInteger returns Decimal?
我正在尝试在 SQLAlchemy 上创建一个类型转换器,它从数据库中获取一个BIGINTEGER
,并将其转换为 Base64 值。
问题是,由于某些原因,来自数据库的值不是int
,而是decimal.Decimal
。 我怀疑这与我使用的 BigInteger 类有关(来自 MySQL 方言而不是来自 SQLAlchemy 的BIGINT
,因为我需要设置 unsigned 参数)。
知道如何从数据库中获取int
而不是decimal.Decimal
吗?
这是我目前的实现:
class B64ID(types.TypeDecorator):
impl = BIGINT # from sqlalchemy.dialects.mysql import BIGINT
def __init__(self, prefix):
self.prefix = prefix
self.impl.unsigned = True
types.TypeDecorator.__init__(self, unsigned=self.impl.unsigned)
def process_bind_param(self, value, dialect=None):
return int(base64.b64decode(value.encode()))
def process_result_value(self, value, dialect=None):
print(value, value.__class__) # Here, in some case, the class is decimal.Decimal!
return base64.b64encode(str(value).encode()).decode()
如何定义来自数据库的值不是decimal.Decimal
,而是int
?
BIGINT 作为 python int 返回给您,而不是 Decimal。 问题很可能是您实际上并未将数据库列创建为 BIGINT。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.