'SQLAlchemy fails to insert decimal only on temp table

Trying to insert into temp tables with SQLAlchemy (plugged into PYODBC/sql server) and inserting more than one row with decimal values and fast_executemany=True throws:

ProgrammingError("(pyodbc.ProgrammingError) ('Converting decimal loses precision', 'HY000')")

This happens only in temp table with fast_executemany=True and multiple rows being inserted at once with one column being decimal. Inserting one at a time, turning fast_executemany off or inserting into a regular table works perfectly.

I've built a simple example:

CONNSTR = "mssql+pyodbc://user:PASSWORD@SERVER?driver=ODBC Driver 17 for SQL Server&trusted_connection=yes"

def test():
    data = [(1, Decimal('41763.9907359278'), Decimal('227367.1749095026')), (1027, Decimal('3117.1592020142'), Decimal('16970.1139430488'))]
    engine = sqlalchemy.create_engine(CONNSTR, fast_executemany=True, connect_args={'connect_timeout': 10})
    #this will fail
    insert(engine, data, "#temp_table_test")
    #this will work
    insert(engine, data, "regular_table_test") 

def insert(engine, data, table_name):
    try:
        with engine.begin() as con:
            con.execute(f"""DROP TABLE IF EXISTS {table_name};""")
            con.execute(f"""
                CREATE TABLE {table_name} (
                    [id_column] INT NULL UNIQUE,
                    [usd_price] DECIMAL(38,20) NULL,
                    [brl_price] DECIMAL(38,20) NULL,
                )
            """)
            sql_insert_prices = f"INSERT INTO {table_name} VALUES (?,?,?)"
            con.execute(sql_insert_prices, data)
            print(f"Insert em {table_name} worked!")
    except Exception as e:
        print(f"{e!r}")
        print(f"Insert em {table_name} failed!")
        

While obviously related to the minimal conversion mechanisms done by fast execute, I can't find out why this runs differently depending on the type of table. Every other question here citing this particular exception is caused by other factors not present here I think so I'm really at a loss.

EDIT: so the original test with just one decimal column ran fine (I assumed reducing the number of columns wouldn't change the output), but adding another decimal column brings me back to square one with the same error message



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source