Fastapi: [BUG] Error in example about connecting with SQLAlchemy

Created on 21 Oct 2019  路  7Comments  路  Source: tiangolo/fastapi

Describe the bug
Facing an error with example __sql_app__ from the tutorial SQL (Relational) Databases.

  File "./sql_app/main.py", line 25, in create_user
    db_user = crud.get_user_by_email(db, email=user.email)
  File "./sql_app/crud.py", line 11, in get_user_by_email
    return db.query(models.User).filter(models.User.email == email).first()
  AttributeError: 'generator' object has no attribute 'query'

To Reproduce
Follow instruction from the example (link above):

  1. Create files into _sql_app_ directory.
  2. Run venv/bin/uvicorn sql_app.main:app --reload
  3. http://127.0.0.1:8000/docs
  4. Execute the _Create user_ operation

Environment:

  • OS: Linux, Ubuntu 18.04.3 LTS
  • Python 3.7.3
  • FastAPI 0.39.0
  • SQLAlchemy 1.3.10
bug

Most helpful comment

I am seeing this same issue in .52 using almost verbatim the doc examples with postgres. Depends is not calling the function but returning what appears to be an instantiate Depends object.

All 7 comments

FastAPI 0.39.0 does not support yield in dependencies, try to upgrade FastApi to 0.42.0.

or see this
https://github.com/tiangolo/fastapi/blob/78272ac1f32bce5c972633006d301d16557eee9e/docs/src/sql_databases/sql_app/main.py

Thanks! This helped.

I am seeing this same issue in .52 using almost verbatim the doc examples with postgres. Depends is not calling the function but returning what appears to be an instantiate Depends object.

The reference previously posted by @peilion seems to no longer be a valid URL. Following the same example as the OP, I am receiving the following error: 'Depends' object has no attribute 'query'. This is because I'm using the db: Session = Depends(get_db) approach to access SessionLocal. If I attempt to get a db session by calling get_db directly, then I receive the 'generator' object has no attribute 'query' error that the OP originally mentioned. This is with FastAPI version .53

Thanks for the help here @peilion ! :clap: :bow:

Thanks for reporting back and closing the issue @rappongy :+1:


@pspierce and @H0r53 if you are still having problems, please create a new issue with a minimal self-contained example with the problem, that way we can check what's happening.

I am seeing this same issue in .52 using almost verbatim the doc examples with postgres. Depends is not calling the function but returning what appears to be an instantiate Depends object.

@pspierce and @H0r53, Did you solve this? I am seeing in 0.58.1. I also tried: instead of using generator, returning the db directly -- same problem.

I am seeing this same issue in .52 using almost verbatim the doc examples with postgres. Depends is not calling the function but returning what appears to be an instantiate Depends object.

@pspierce and @H0r53, Did you solve this? I am seeing in 0.58.1. I also tried: instead of using generator, returning the db directly -- same problem.

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

engine = create_engine('postgresql://postgres:postgres@localhost:5432/postgres', echo=True)
default_session = scoped_session(sessionmaker(autocommit=False,
                                         autoflush=False,
                                         bind=engine))

def get_database():
    db = default_session()
    try:
        yield db
    finally:
        db.close()

First error I got:

AttributeError: 'Depends' object has no attribute 'query'

from database.data import get_database, scoped_session
from database.models.reference import Reference
from fastapi import Depends


def get_references(db: scoped_session = Depends(get_database())):
    references = db.query(Reference).all()
    return references

Without Depends:

AttributeError: 'generator' object has no attribute 'query'

def get_references(db: scoped_session = get_database()):
Was this page helpful?
0 / 5 - 0 ratings