Airflow PostgresHook从未提交的插入语句返回ID [英] Airflow PostgresHook returning an ID from an Insert statement not committing
问题描述
我在气流运算符中使用PostgresHook。
I am using PostgresHook in an Airflow operator.
pg_hook = PostgresHook(postgres_conn_id='postgres_default')
insert_activities_sql = "INSERT INTO activities (---) VALUES (---) RETURNING id "
activity_results = pg_hook.get_first(insert_activities_sql,parameters=insert_activities_params)
这确实返回ID,但记录未提交到活动表中。我试过运行get_records和get_first且都没有提交。
This does return the Id but the record is not committed into the activities table. I have tried running get_records and get_first and neither commit.
。运行提交但不返回结果ID。
.run commits but does not return the results id.
这是插入记录然后返回ID的正确方法吗?
Is this the correct way to insert a record and then return the id?
推荐答案
您可以调用 get_autocommit()
检查是否启用了自动提交,然后 set_autocommit()
进行显式启用。看来 Airflow DBApiHook 天真地假设您在获取记录时不会提交任何内容。
You can call get_autocommit()
to check whether or not autocommit is enabled and then set_autocommit()
to enable explicitly. It appears that the Airflow DBApiHook is naively assuming you will not be committing anything when fetching records. Setting it explicitly should resolve that issue.
如果希望对正在发生的事情有更多控制,可以调用 get_conn()
或 get_cursor()
并复制 run()
和<$ c内发生的逻辑$ c> get_first()手动提交。
If you would like even more control over what is happening, you can call get_conn()
or get_cursor()
and replicate the logic that is happening inside of run()
and get_first()
to manually commit.
这篇关于Airflow PostgresHook从未提交的插入语句返回ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!