question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Error in example about connecting with SQLAlchemy

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
pspiercecommented, Mar 23, 2020

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.

6reactions
H0r53commented, Apr 1, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error Messages - SQLAlchemy 1.4 Documentation
The ProgrammingError is sometimes raised by drivers in the context of the database connection being dropped, or not being able to connect to...
Read more >
Connection problems with SQLAlchemy and multiple processes
The issue stems from the forked child process inheriting the live global session , which is holding on to a Connection . When...
Read more >
Use Flask and SQLalchemy, not Flask-SQLAlchemy!
This is often caused by a session or database connection being unavailable to part of your app. This error effectively breaks your app...
Read more >
Define Relationships Between SQLAlchemy Data Models
SQLAlchemy's ORM easily defines data models with relationships such as one-to-one, one-to-many, and many-to-many relationships.
Read more >
SQLAlchemy won't connect correctly but pyodbc and pymssql ...
However, when I attempt the same connection and query through SQLAlchemy either using an ORM or direct SQL, it fails with the following...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found