具有插入规则的SQLAlchemy和PostGreSQL不执行任何操作(重复)

时间:2018-08-31 08:24:13

标签: python postgresql flask-sqlalchemy rules

我正在使用Flask SQLAlchemy和PostGreSQL创建rest api。

我的餐桌记录

table_history

class H(db.Model):
    __tablename__ = "history"
    id = db.Column(
        db.Integer,
        primary_key=True,
        autoincrement=True
    )
    one_data = db.Column(
        db.Integer,
        nullable=False
    )
    count = db.Column(
        db.Integer
    )

我的规则:

CREATE OR REPLACE RULE history_no_duplicate 
  AS ON INSERT TO history WHERE 
    EXISTS ( SELECT 1 FROM history WHERE count = NEW.count)
  DO INSTEAD NOTHING

直接在SQL中,所有插入都可以正常工作。

我的用于插入数据的SQLAlchemy代码:

def save(history_data):
    db.engine.execute(
        db.insert(
            History.__table__, history_data
        )
    )
save({
    "one_data": 1,
   "count": 1
})

没有规则,此脚本有效,但是当我插入规则时,SQL Alchemy返回错误:

sqlalchemy.exc.NotSupportedError: (psycopg2.NotSupportedError) cannot perform INSERT RETURNING on relation "history"
HINT:  You need an unconditional ON INSERT DO INSTEAD rule with a RETURNING clause.

0 个答案:

没有答案