如何使SQLAlchemy插入与Postgres多处理防止upsert触发器一起工作?

时间:2016-03-18 10:39:41

标签: python postgresql sqlalchemy

我有多处理应用程序需要upsert(插入,如果存在更新)功能。

我决定使用触发器解决方案进行upsert。 (为每个启用upsert的表名为is_upsert添加附加列,并在触发器中检查此字段,如果为false,则执行正常插入,但如果为true,则执行upsert逻辑 - 尝试更新,如果失败,因为记录没有你尝试插入就存在了。

这是触发逻辑:

CREATE OR REPLACE FUNCTION upsert_trigger_function_{table}()
RETURNS TRIGGER AS $upsert_trigger_function$
    DECLARE
    row record;
    BEGIN
    RAISE NOTICE 'upsert trigger fired, upsert is %%', NEW.{upsert_column};
    IF NEW.{upsert_column} THEN
        NEW.{upsert_column} := false;
        LOOP
            UPDATE {table} SET
                {update_set}
            WHERE
                {update_where}
            ;
            IF found THEN
                RETURN NULL;
            END IF;
            BEGIN
                INSERT INTO {table} SELECT NEW.*;
                RETURN NULL;
            EXCEPTION WHEN unique_violation THEN
                -- loop
            END;
        END LOOP;
        RETURN NULL;
    ELSE
        RETURN NEW;
    END IF;
    END;
$upsert_trigger_function$ LANGUAGE plpgsql;

测试对象,(add_upsert只是使上面的触发器安装):

class SimpleItem(PipelinesBase):
    __tablename__ = 'simple_item'

    id = Column(BigInteger, primary_key=True)
    item_type = Column(String, nullable=False, unique=True)
    quantity = Column(Integer, nullable=False)
    price = Column(Float, nullable=False)
    in_stock = Column(Boolean, nullable=False)
    arrived = Column(Date)
    sys_time = Column(
    TSTZRANGE,
    nullable=False,
    server_default=text("TSTZRANGE(now(), null)"),
    )
    _upsert = Column(Boolean, nullable=False, server_default=text('false'))
    _type_identifier = 1400
add_upsert(SimpleItem, ['item_type'])

测试脚本

from sqlalchemy.engine import create_engine
from pipelines.settings_proxy import TEST_DB
from sqlalchemy.orm.session import sessionmaker
from test_pipelines.test_persistence.mock_items import SimpleItem
from test_pipelines.test_persistence.helpers import random_simple_item

def main():
    engine = create_engine(TEST_DB)

    values = random_simple_item(_upsert=True)

    session = sessionmaker(engine)()

    si = SimpleItem(**values)
    session.add(si)
    session.commit()

    si = SimpleItem(**values)
    si.price = 1
    session.merge(si)
    session.commit()

使用SQL语句时它可以正常工作,但当我和SQLAlchemy ORM一起使用时,它会添加对象

Traceback (most recent call last):
  File "pipelines/persistence/experiment_with_upsert_field.py", line 59, in <module>
    main()
  File "pipelines/persistence/experiment_with_upsert_field.py", line 27, in main
    session.commit()
  File "/home/sebastian/local/virtualenvs/perception/lib/python3.4/site-packages/sqlalchemy/orm/session.py", line 801, in commit
    self.transaction.commit()
  File "/home/sebastian/local/virtualenvs/perception/lib/python3.4/site-packages/sqlalchemy/orm/session.py", line 392, in commit
    self._prepare_impl()
  File "/home/sebastian/local/virtualenvs/perception/lib/python3.4/site-packages/sqlalchemy/orm/session.py", line 372, in _prepare_impl
    self.session.flush()
  File "/home/sebastian/local/virtualenvs/perception/lib/python3.4/site-packages/sqlalchemy/orm/session.py", line 2019, in flush
    self._flush(objects)
  File "/home/sebastian/local/virtualenvs/perception/lib/python3.4/site-packages/sqlalchemy/orm/session.py", line 2137, in _flush
    transaction.rollback(_capture_exception=True)
  File "/home/sebastian/local/virtualenvs/perception/lib/python3.4/site-packages/sqlalchemy/util/langhelpers.py", line 60, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/home/sebastian/local/virtualenvs/perception/lib/python3.4/site-packages/sqlalchemy/util/compat.py", line 184, in reraise
    raise value
  File "/home/sebastian/local/virtualenvs/perception/lib/python3.4/site-packages/sqlalchemy/orm/session.py", line 2101, in _flush
    flush_context.execute()
  File "/home/sebastian/local/virtualenvs/perception/lib/python3.4/site-packages/sqlalchemy/orm/unitofwork.py", line 373, in execute
    rec.execute(self)
  File "/home/sebastian/local/virtualenvs/perception/lib/python3.4/site-packages/sqlalchemy/orm/unitofwork.py", line 532, in execute
    uow
  File "/home/sebastian/local/virtualenvs/perception/lib/python3.4/site-packages/sqlalchemy/orm/persistence.py", line 174, in save_obj
    mapper, table, insert)
  File "/home/sebastian/local/virtualenvs/perception/lib/python3.4/site-packages/sqlalchemy/orm/persistence.py", line 800, in _emit_insert_statements
    execute(statement, params)
  File "/home/sebastian/local/virtualenvs/perception/lib/python3.4/site-packages/sqlalchemy/engine/base.py", line 914, in execute
    return meth(self, multiparams, params)
  File "/home/sebastian/local/virtualenvs/perception/lib/python3.4/site-packages/sqlalchemy/sql/elements.py", line 323, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/home/sebastian/local/virtualenvs/perception/lib/python3.4/site-packages/sqlalchemy/engine/base.py", line 1010, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/home/sebastian/local/virtualenvs/perception/lib/python3.4/site-packages/sqlalchemy/engine/base.py", line 1159, in _execute_context
    result = context._setup_crud_result_proxy()
  File "/home/sebastian/local/virtualenvs/perception/lib/python3.4/site-packages/sqlalchemy/engine/default.py", line 828, in _setup_crud_result_proxy
    self._setup_ins_pk_from_implicit_returning(row)
  File "/home/sebastian/local/virtualenvs/perception/lib/python3.4/site-packages/sqlalchemy/engine/default.py", line 893, in _setup_ins_pk_from_implicit_returning
    for col in table.primary_key
  File "/home/sebastian/local/virtualenvs/perception/lib/python3.4/site-packages/sqlalchemy/engine/default.py", line 891, in <listcomp>
    for col, value in [
TypeError: 'NoneType' object is not subscriptable

提高sqlalchemy.engine.default的深度。我很确定这是因为我的触发器在执行UPSERT时返回NULL并且SQLAlchemy尝试使用RETURNING语句传播带有插入ID的对象。哪个显然失败了,因为它无法从它的下级INSERT / UPDATE中获取正确的ID,同时阻止正常的正常插入。

请注意,我已经测试了upsert作为特殊功能,这对我不起作用,因为我牺牲SQLAlchemy帮助更新复杂项目(与其他项目有关系的那些)。

所以这是我的问题:如何告诉SQLAlchemy避免加载插入的对象ID?

1 个答案:

答案 0 :(得分:1)

经过研究和测试后,我发现它无法在SQLAlchemy ORM中完成。但是,可以通过将inline关键字参数设置为True来在SQLAlchemy Core中完成:

engine.execute(
    SimpleItem.__table__.insert(inline=True),
    values
)

values['price'] = 1
engine.execute(
    SimpleItem.__table__.insert(inline=True),
    values
)