SQLAlchemy PostgreSQL UPSERT数组值引发UnsupportedCompilationError

时间:2017-04-11 16:55:38

标签: python postgresql sqlalchemy

尝试对PostgreSQL的值数组执行UPSERT> 9.5。

尝试在SQLALchemy文档中构建语句,但没有关于如何为数组而不是单行执行此操作的说明。 insert语句构建正确,所以我想可以使用on_conflict_do_update函数来实现。

拥有此代码:

stock_table = Table("stock_history", metadata,
            Column('date', sqlalchemy.types.NVARCHAR(length=255), primary_key=True),
            Column('product_id', sqlalchemy.types.INTEGER(), primary_key=True),
            Column('product_sku', sqlalchemy.types.NVARCHAR(length=255)), 
            Column('on_hand_qty',  sqlalchemy.dialects.postgresql.DOUBLE_PRECISION()),
            Column('available_qty',  sqlalchemy.dialects.postgresql.DOUBLE_PRECISION()),
            Column('output_qty', sqlalchemy.dialects.postgresql.DOUBLE_PRECISION())
        )
stock_today = pandas.read_sql_query(queryStock, odoo_engine)
insert_stmt = sqlalchemy.dialects.postgresql.insert(stock_table).values(stock_today)
upser_stmt = insert_stmt.on_conflict_do_update(
            index_elements=['date', 'product_id'],
            set_=stock_today.to_dict(orient='dict')
        )

我收到以下错误:

AttributeError:'StrSQLCompiler'对象没有属性'visit_on_conflict_do_update'

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "pompeiiETL.py", line 15, in <module>
        pompeiiJobs.runStockJob(dwh_engine, odoo_prod_engine)
      File "/Users/alex/Development/DataLab/pompeii-datalab/pompeiiETL/jobs.py", line 54, in runStockJob
        print(upser_stmt)
      File "/Users/alex/Development/DataLab/ETLenv/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 446, in __str__
        return str(self.compile())
      File "<string>", line 1, in <lambda>
      File "/Users/alex/Development/DataLab/ETLenv/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 436, in compile
        return self._compiler(dialect, bind=bind, **kw)
      File "/Users/alex/Development/DataLab/ETLenv/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 442, in _compiler
        return dialect.statement_compiler(dialect, self, **kw)
      File "/Users/alex/Development/DataLab/ETLenv/lib/python3.6/site-packages/sqlalchemy/sql/compiler.py", line 435, in __init__
        Compiled.__init__(self, dialect, statement, **kwargs)
      File "/Users/alex/Development/DataLab/ETLenv/lib/python3.6/site-packages/sqlalchemy/sql/compiler.py", line 216, in __init__
        self.string = self.process(self.statement, **compile_kwargs)
      File "/Users/alex/Development/DataLab/ETLenv/lib/python3.6/site-packages/sqlalchemy/sql/compiler.py", line 242, in process
        return obj._compiler_dispatch(self, **kwargs)
      File "/Users/alex/Development/DataLab/ETLenv/lib/python3.6/site-packages/sqlalchemy/sql/visitors.py", line 81, in _compiler_dispatch
        return meth(self, **kw)
      File "/Users/alex/Development/DataLab/ETLenv/lib/python3.6/site-packages/sqlalchemy/sql/compiler.py", line 2041, in visit_insert
        insert_stmt._post_values_clause, **kw)
      File "/Users/alex/Development/DataLab/ETLenv/lib/python3.6/site-packages/sqlalchemy/sql/compiler.py", line 242, in process
        return obj._compiler_dispatch(self, **kwargs)
      File "/Users/alex/Development/DataLab/ETLenv/lib/python3.6/site-packages/sqlalchemy/sql/visitors.py", line 79, in _compiler_dispatch
        raise exc.UnsupportedCompilationError(visitor, cls)
    sqlalchemy.exc.UnsupportedCompilationError: Compiler <sqlalchemy.sql.compiler.StrSQLCompiler object at 0x105b55be0> can't render element of type <class 'sqlalchemy.dialects.postgresql.dml.OnConflictDoUpdate'>

我错了什么?有没有更好的方法来进行upsert?

谢谢!

1 个答案:

答案 0 :(得分:3)

你正在尝试获取Insert的字符串表示形式 - 如果我们写的话没有正确绑定的对象

db_uri = make_url('your-postgres-db-uri-here')
engine = create_engine(db_uri)
upser_stmt.bind = engine
print(upser_stmt)

它有效

我们还可以使用bind指定

创建insert语句
insert_stmt = sqlalchemy.dialects.postgresql.insert(stock_table,
                                                    bind=engine).values(stock_today)
upser_stmt = insert_stmt.on_conflict_do_update(
    index_elements=['date', 'product_id'],
    set_=stock_today.to_dict(orient='dict')
)