Skip to content

Commit

Permalink
Efficient single query insert into through table
Browse files Browse the repository at this point in the history
  • Loading branch information
uranusjr committed Sep 20, 2024
1 parent 7bd6274 commit a106a8a
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions airflow/datasets/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,15 @@ def register_dataset_change(
cls.logger().warning("DatasetModel %s not found", dataset)
return None

dataset_model.aliases = session.scalars(
select(DatasetAliasModel).where(DatasetAliasModel.name.in_(alias.name for alias in aliases))
# This INSERTs directly into the association table of the many-to-many
# dataset_model.aliases relationship. I don't know how to do it in ORM.
session.execute(
DatasetAliasModel.datasets.prop.secondary.insert().from_select(
["alias_id", "dataset_id"],
select(DatasetAliasModel.id, dataset_model.id).where(
DatasetAliasModel.name.in_(alias.name for alias in aliases)
),
)
)

event_kwargs = {
Expand Down

0 comments on commit a106a8a

Please sign in to comment.