mcfunley / mcfunley/pugsql

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

Open
#48 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
777
Forks
31
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with the tables and engine setup in the issue, then inspect the @compiles(BindParameter) hook in pugsql.statement alongside SQLAlchemy's MetaData.create_all() path. Determine why the hook recurses during table creation; done means create_all completes without RecursionError while preserving the described pugsql behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, sqlalchemy
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.