【问题标题】:SQLAlchemy ORM to convert input data on insertSQLAlchemy ORM 在插入时转换输入数据
【发布时间】:2019-11-21 17:19:38
【问题描述】:

我想使用 SQLAlchemy ORM 将记录加载到 Postgres,但出现此错误: psycopg2.ProgrammingError:无法适应类型'lxml.objectify.StringElement'

我有模型:

class B2bProduct(Base):
    __tablename__ = 'b2b_product'
    code = Column(String, primary_key=True)

当我尝试插入产品列表时(值类型为'lxml.objectify.StringElement'的字典列表):

with session_scope() as s:
    s.bulk_insert_mappings(B2bProduct, prod_list)

我收到此错误:

psycopg2.ProgrammingError: can't adapt type 'lxml.objectify.StringElement'

作为一种解决方法,我可以在批量插入之前将所有值转换为 Python 字符串。但是,我更喜欢在 B2bProduct 类定义中进行强制转换。是否可以使用声明性 ORM,以便它会自动将我给它的任何值转换为 Python 字符串(在实际插入之前)?

类似这样的:

code = Column(String, primary_key=True, convert_to_string=True)

我不知道,但我可能会询问将进行转换的 TypeEngine。能给点建议吗?

【问题讨论】:

标签: python postgresql sqlalchemy


【解决方案1】:

所以我发现答案是 Raito 暗示的 TypeDecorator。谢谢。

class Float2(types.TypeDecorator):
    ''' Float Type that replaces commas with  dots on input '''
    impl = types.Float
    def process_bind_param(self, value, dialect):  # insert
        return value.replace(',','.')
    def process_result_value(self, value, dialect):  # select
        return value

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-06
    • 2011-04-09
    • 2021-06-25
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    相关资源
    最近更新 更多