Skip to content

[BUG] Using sqlalchemy's Metadata.create_all() raises RecursionError: maximum recursion depth exceeded #48

@nicksspirit

Description

@nicksspirit

I am using sqlalchemy core and pugsql, my use case is to use sqlalchemy core's Table and Column classes to create tables and manage migrations via alembic while puqsql will be used to do other types of queries.

I have these tables

import sqlalchemy

metadata = sa.MetaData()

PSBudgetForecast = sa.Table(
    "PS_Budget_Forecast",
    metadata,
    sa.Column("id", sa.Integer, primary_key=True),
    sa.Column("FiscalPeriod", sa.String(32), unique=True),
    sa.Column("Budget", sa.Numeric),
    sa.Column("ForecastTTE", sa.Numeric),
    sa.Column("ForcastMobility", sa.Numeric),
    sa.Column("ForecastOEM", sa.Numeric),
    sa.Column("ForecastTotal", sa.Numeric),
)

DatetimeLogTable = sa.Table(
    "PS_DateTime_Log",
    metadata,
    sa.Column("id", sa.Integer, primary_key=True, autoincrement=False),
    sa.Column("LastUpdateDate", sa.DateTime(timezone=True), server_default=sa.func.now()),
)

After I import puqsql and sqlalchemy's create_engine method I create and instantiate my engine. Then I procced to create an sql connection to the db and pass it to MetaData.create_all.

import sqlalchemy as sa
import puqsql

engine = sa.create_engine(DB_CONN_STR)
sqlmodule = pugsql.module(SQL_PATH)
sqlmodule.setengine(engine)


with engine.connect() as conn:
    metadata.create_all(conn)

It is after calling create_all that I get a huge stacktrace and then the final message about maximum recursion depth being exceeded.

I managed to track down the root of the issue with is this piece of code in the pugsql.statment module

@compiles(BindParameter)
def _visit_bindparam(element, compiler, **kw):
    cc = getattr(_locals, 'compile_context', None)
    if cc:
        if _is_expanding_param(element, cc):
            element.expanding = True
    return compiler.visit_bindparam(element)

I am not very familiar with the internals of sqlalchemy but what does this function do and why is it causing a recursion exceeded runtime error?

Any insight will be much appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions