Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nicksspirit opened this issue Feb 1, 2021 · 0 comments

Comments

@nicksspirit
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant