[BUG] Error in example about connecting with SQLAlchemy
See original GitHub issueDescribe 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):
- Create files into sql_app directory.
- Run
venv/bin/uvicorn sql_app.main:app --reload
- http://127.0.0.1:8000/docs
- 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:
- Created 4 years ago
- Comments:11 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 thedb: Session = Depends(get_db)
approach to accessSessionLocal
. If I attempt to get a db session by callingget_db
directly, then I receive the'generator' object has no attribute 'query'
error that the OP originally mentioned. This is with FastAPI version .53