Sqlalchemy AttributeError: 'NoneType' object has no attribute 'dispatch'

Viewed 35

Hello!

When session commit, infrequently happens this error.

Interestingly, this error can occur in any part of the project and rarely comes out intermittently.

We use fastapi structure with sqlalchemy

Session create:

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

from app.core.config import settings

engine = create_engine(
    settings.SQLALCHEMY_DATABASE_URI,
    pool_size=10,  # default in SQLAlchemy
    max_overflow=5,  # default in SQLAlchemy
    connect_args={"application_name": settings.PROJECT_NAME},
    echo=settings.POSTGRES_ECHO,
)

SessionLocal = scoped_session(sessionmaker(bind=engine))
from typing import Generator, List
from app.db.session import SessionLoca


def get_db() -> Generator:
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()
@router.put("/")
def endpoint(
        data: schemas.UpdateSchemas,
        db: Session = Depends(deps.get_db),
) -> Any:

Error

Traceback (most recent call last):
  File "/app/app/api/api_v1/endpoints/otp.py", line 24, in check_otp
    customer = crud.customer.create(
  File "/app/app/crud/base.py", line 56, in create
    db.commit()
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 1451, in commit
    self._transaction.commit(_to_root=self.future)
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 843, in commit
    self.close()
  File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 928, in close
    self.session.dispatch.after_transaction_end(self.session, self)
AttributeError: 'NoneType' object has no attribute 'dispatch'
0 Answers
Related