{"id":914,"hash":"f8adedd470097594996c529b5311d6089ce73f8af89f2d3dc1c4856e1a7e3bc8","pattern":"AttributeError: &#39;Depends&#39; object has no attribute &#39;query&#39; FastAPI","full_message":"So I am trying to write simple function here, but every time I run swagger, I got above mentioned error.\n\nHere's my function:\n\ndef authenticate_user(username: str, password: str, db: Session = Depends(bd.get_db)):\n    user = db.query(bd.User.username).filter(username == username).first()\n    if not user:\n        return False\n    if not verify_password(password, user.password_hash):\n        return False\n    return user\n\nand here's my get_db function it is pretty standard:\n\ndef get_db():\n    db = SessionLocal()\n    try:\n        yield db\n    finally:\n        db.close()\n\nI've noticed that Depends(bd.get_db) works perfectly fine within endpoint functions (the ones with @app.post/@app.get decorators), but somehow doesn't work within plain functions.\n\nApparently, I don't quite understand the concept of dependency injections, but I can't quite grasp it yet.","ecosystem":"pypi","package_name":"dependency-injection","package_version":null,"solution":"This page helped me a lot, https://github.com/tiangolo/fastapi/issues/1693#issuecomment-665833384\n\nyou can't use Depends in your own functions, it has to be in FastAPI\nfunctions, mainly routes. You can, however, use Depends in your own\nfunctions when that function is also a dependency, so could can have a\nchain of functions.\n\nEg, a route uses Depends to resolve a 'getcurrentuser', which also\nuses Depends to resolve 'getdb', and the whole chain will be resolved.\nBut if you then call 'getcurrentuser' without using Depends, it won't\nbe able to resolve 'getdb'.\n\nWhat I do is get the DB session from the route and then pass it down\nthrough every layer and function. I believe this is also better\ndesign.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/68981634/attributeerror-depends-object-has-no-attribute-query-fastapi","votes":26,"created_at":"2026-04-19T04:51:59.337815+00:00","updated_at":"2026-04-19T04:51:59.337815+00:00"}