问题
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