Airflow PostgresHook returning an ID from an Insert statement not committing

纵然是瞬间 提交于 2019-12-12 22:22:39

问题


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)

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.

.run commits but does not return the results id.

Is this the correct way to insert a record and then return the id?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/53273338/airflow-postgreshook-returning-an-id-from-an-insert-statement-not-committing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!