MSSQL dialect generates wrong syntax for CAST #9914
Answered
by
zzzeek
prudnikov
asked this question in
Usage Questions
-
I generate CTE based on the Table metadata and data in JSON. engine = create_engine(
f"mssql+pyodbc://{username}:{password}@{hostname}/{database_name}?"
"driver=ODBC+Driver+17+for+SQL+Server",
echo=False,
)
table_metadata = sa.MetaData(schema="dbo")
destination_table = sa.Table(table_name, table_metadata, autoload_with=engine)
def make_cols(table: sa.Table, row: dict[str, typing.Any]):
columns = []
for col in table.columns:
assert isinstance(col, sa.Column)
value = sa.literal(row.get(col.key.lower()))
columns.append(sa.cast(value, col.type).label(str(col.key)))
# Not sure if next line will produce the same results as previous one
# columns.append(col.cast(value).label(str(col.key)))
return columns
stmts = [
sa.select(
*make_cols(destination_table, obj)
)
for obj in json_objects
]
data_cte = sa.union_all(*stmts).cte(name="MyTable_CTE") This produces the following query
which is invalid MS SQL Server query. It should be
Am I doing something wrong or it can be considered as a bug? |
Beta Was this translation helpful? Give feedback.
Answered by
zzzeek
Jun 8, 2023
Replies: 1 comment 8 replies
-
May be there is a workaround that tells SA not to add COLLATE at all? I need to make this work ASAP, any help appreciated. Thanks. |
Beta Was this translation helpful? Give feedback.
8 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yes make a function that fixes the cast: