sqlalchemy创建临时表

时间:2018-08-28 12:27:33

标签: postgresql sqlalchemy

我用sqlalchemy创建了一个临时表(具有基础的postgres数据库),该表将与数据库表连接。但是,在某些情况下,如果值''为空,则postgres会引发错误:

failed to find conversion function from unknown to text

SqlAlchemy将所有内容组装到以下上下文中

[SQL: 'WITH temp_table AS \n(SELECT %(param_1)s AS id, %(param_2)s AS email, %(param_3)s AS phone)\n SELECT campaigns_contact.id, campaigns_contact.email, campaigns_contact.phone \nFROM campaigns_contact JOIN temp_table ON temp_table.id = campaigns_contact.id AND temp_table.email = campaigns_contact.email AND temp_table.phone = campaigns_contact.phone'] [parameters: {'param_1': 83, 'param_2': '', 'param_3': '+1234567890'}]

我按照以下步骤组装临时表

stmts = []
for row in import_data:
    row_values = [literal(row[value]).label(value) for value in values]
    stmts.append(select(row_values))

subquery = union_all(*stmts)
subquery = subquery.cte(name="temp_table")

问题似乎出在这里

...%(param_2)s AS email...

替换param_2后会导致

...'' AS email...

这将导致上述错误。 解决该问题的一种方法是执行强制转换

...''::text AS email...

但是,我不知道如何使用sqlalchemy执行::text强制转换!?

0 个答案:

没有答案