我有这样的插入语句:
insert = MyTable.insert()
row = {"foo": 42}
result = cxn.execute(insert, row)
但是 Postgres 触发器将插入重定向mytable
到新表,因此插入语句实际上并没有返回任何内容:
# INSERT INTO mytable VALUES (DEFAULT, 'stuff') RETURNING LASTVAL();
lastval
---------
(0 rows)
这会导致 SQLAlchemy (0.8b2) 引发TypeError: 'NoneType' is not subscriptable
. DefaultExecutionContext
当尝试获取插入行的主键时,似乎正在发生这种情况。
这是一个应该报告的错误吗?有什么办法可以解决这个问题,告诉 SQLAlchemy 不要期望返回值?
追溯
… snip…
File "myfile.py", line 85, in store
result = cxn.execute(insert, row)
File ".../sqlalchemy/engine/base.py", line 664, in execute
params)
File ".../sqlalchemy/engine/base.py", line 764, in _execute_clauseelement
compiled_sql, distilled_params
File ".../sqlalchemy/engine/base.py", line 899, in _execute_context
context._fetch_implicit_returning(result)
File ".../sqlalchemy/engine/default.py", line 697, in _fetch_implicit_returning
ipk.append(row[c])
TypeError: 'NoneType' object is not subscriptable
Postgres 触发器
CREATE OR REPLACE FUNCTION mytable_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO mytable_cur VALUES (NEW.*);
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER mytable_insert_trigger
BEFORE INSERT ON mytable
FOR EACH ROW EXECUTE PROCEDURE mytable_insert_trigger();