@@ -489,15 +489,14 @@ def migrate(
489489 allow_destructive_snapshots = allow_destructive_snapshots or set ()
490490 allow_additive_snapshots = allow_additive_snapshots or set ()
491491 snapshots_by_name = {s .name : s for s in snapshots .values ()}
492- snapshots_with_data_objects = [snapshots [s_id ] for s_id in target_data_objects ]
493492 with self .concurrent_context ():
494493 # Only migrate snapshots for which there's an existing data object
495494 concurrent_apply_to_snapshots (
496- snapshots_with_data_objects ,
495+ snapshots_by_name . values () ,
497496 lambda s : self ._migrate_snapshot (
498497 s ,
499498 snapshots_by_name ,
500- target_data_objects [ s .snapshot_id ] ,
499+ target_data_objects . get ( s .snapshot_id ) ,
501500 allow_destructive_snapshots ,
502501 allow_additive_snapshots ,
503502 self .get_adapter (s .model_gateway ),
@@ -747,11 +746,15 @@ def _evaluate_snapshot(
747746 adapter .execute (model .render_pre_statements (** render_statements_kwargs ))
748747
749748 if not target_table_exists or (model .is_seed and not snapshot .intervals ):
750- columns_to_types_provided = (
749+ # Only create the empty table if the columns were provided explicitly by the user
750+ should_create_empty_table = (
751751 model .kind .is_materialized
752752 and model .columns_to_types_
753753 and columns_to_types_all_known (model .columns_to_types_ )
754754 )
755+ if not should_create_empty_table :
756+ # Or if the model is self-referential and its query is fully annotated with types
757+ should_create_empty_table = model .depends_on_self and model .annotated
755758 if self ._can_clone (snapshot , deployability_index ):
756759 self ._clone_snapshot_in_dev (
757760 snapshot = snapshot ,
@@ -764,7 +767,7 @@ def _evaluate_snapshot(
764767 )
765768 runtime_stage = RuntimeStage .EVALUATING
766769 target_table_exists = True
767- elif columns_to_types_provided or model .is_seed or model .kind .is_scd_type_2 :
770+ elif should_create_empty_table or model .is_seed or model .kind .is_scd_type_2 :
768771 self ._execute_create (
769772 snapshot = snapshot ,
770773 table_name = target_table_name ,
@@ -1059,7 +1062,7 @@ def _migrate_snapshot(
10591062 adapter : EngineAdapter ,
10601063 deployability_index : DeployabilityIndex ,
10611064 ) -> None :
1062- if not snapshot .requires_schema_migration_in_prod :
1065+ if not snapshot .is_model or snapshot . is_symbolic :
10631066 return
10641067
10651068 deployability_index = DeployabilityIndex .all_deployable ()
@@ -1081,20 +1084,32 @@ def _migrate_snapshot(
10811084 ):
10821085 table_exists = False
10831086
1087+ rendered_physical_properties = snapshot .model .render_physical_properties (
1088+ ** render_kwargs
1089+ )
1090+
10841091 if table_exists :
10851092 self ._migrate_target_table (
10861093 target_table_name = target_table_name ,
10871094 snapshot = snapshot ,
10881095 snapshots = snapshots ,
10891096 deployability_index = deployability_index ,
10901097 render_kwargs = render_kwargs ,
1091- rendered_physical_properties = snapshot .model .render_physical_properties (
1092- ** render_kwargs
1093- ),
1098+ rendered_physical_properties = rendered_physical_properties ,
10941099 allow_destructive_snapshots = allow_destructive_snapshots ,
10951100 allow_additive_snapshots = allow_additive_snapshots ,
10961101 run_pre_post_statements = True ,
10971102 )
1103+ else :
1104+ self ._execute_create (
1105+ snapshot = snapshot ,
1106+ table_name = snapshot .table_name (is_deployable = True ),
1107+ is_table_deployable = True ,
1108+ deployability_index = deployability_index ,
1109+ create_render_kwargs = render_kwargs ,
1110+ rendered_physical_properties = rendered_physical_properties ,
1111+ dry_run = True ,
1112+ )
10981113
10991114 def _migrate_target_table (
11001115 self ,
0 commit comments