我正在尝试在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?
发布于 2020-11-04 23:11:46
BIGINT作为python int返回给您,而不是Decimal。问题很可能是您实际上并没有将数据库列创建为BIGINT。
https://stackoverflow.com/questions/64682477
复制相似问题