[Python] sqlAlchemy to_sql with azure function returning collation error, but work without...

Discussão em 'Python' iniciado por Stack, Abril 3, 2025.

  1. Stack

    Stack Membro Participativo

    I have written a python method to form a pandas df and upsert it into sqlserver. This method works perfectly fine when I try to execute the python file alone. But it throws collation exception when I try to run via Azure functions.

    Here is my code:

    import pandas as pd
    from sqlalchemy import create_engine, types, inspect, MetaData, Table

    eng = sqlAlchemy_getConnection(sql_alchemy_connection_url)
    inspector = inspect(eng)
    target_table = "my_users"
    get_colss = inspector.get_columns(target_table,"dbo")
    dtype_mapping = {
    column['name']: column['type']
    for column in get_colss
    }
    src_df.to_sql(temp_table,sqlAlchemy_conn,schema="dbo",if_exists='replace',index=False,dtype=dtype_mapping)


    Error when trying to execute from Azure function:


    Exception user_name (VARCHAR(100) COLLATE "SQL_Latin1_General_CP1_CI_AS") not a string
    ​

    Drivers:


    ODBC Driver 17 for SQL Server
    ​

    What could be the cause of this issue? Can anyone help please. Thanks in advance.

    Continue reading...

Compartilhe esta Página