@@ -70,6 +70,7 @@ def create(
70
70
select : sa .Select ,
71
71
* ,
72
72
name : str = "" ,
73
+ primary_key : Tuple [str , ...] = (),
73
74
indexes : Iterable [Tuple [str , ...]] = (),
74
75
) -> Tuple [sa .Table , int ]:
75
76
"""
@@ -83,6 +84,9 @@ def create(
83
84
schema: The schema to create the temporary table within. For some dialects
84
85
temporary tables always exist in their own schema and this parameter
85
86
will be ignored.
87
+ primary_key: If set will mark the set of columns passed as primary keys in
88
+ the temporary table. This tuple should match a subset of the
89
+ column names in the select query.
86
90
indexes: creates an index on each tuple of columns listed. This is useful
87
91
if future queries are likely to reference these columns.
88
92
@@ -106,7 +110,10 @@ def create(
106
110
metadata ,
107
111
schema = temp_schema ,
108
112
prefixes = ["TEMPORARY" ],
109
- * (sa .Column (col .name , col .type ) for col in select .selected_columns ),
113
+ * (
114
+ sa .Column (col .name , col .type , primary_key = col .name in primary_key )
115
+ for col in select .selected_columns
116
+ ),
110
117
)
111
118
try :
112
119
metadata .create_all (conn )
@@ -120,6 +127,8 @@ def create(
120
127
raise
121
128
122
129
for idx , index_cols in enumerate (indexes ):
130
+ if index_cols == primary_key :
131
+ continue
123
132
# For some dialects/data types we may not be able to construct an index. We just do our
124
133
# best here instead of hard failing.
125
134
try :
@@ -891,6 +900,7 @@ def _materialize_tables(
891
900
schema ,
892
901
table_q ,
893
902
name = table_name ,
903
+ primary_key = table .primary_key ,
894
904
indexes = joined_columns [(schema , table_name )],
895
905
)
896
906
)
@@ -914,6 +924,7 @@ def _materialize_tables(
914
924
schema ,
915
925
meta .temp_tables [(schema , table_name , 0 )].select (),
916
926
name = table_name ,
927
+ primary_key = table .primary_key ,
917
928
indexes = joined_columns [(schema , table_name )],
918
929
)
919
930
)
0 commit comments