'SQL ALCHEMY- FAST API- API MODULE OBJECT NOT CALLABLE

end point to test sql alchemy. Error reads: module object is not callable? also the query word isn't highlighted. The tutorial says I should do db.query(models.Posts).all() but that's not working either. normally vs code highlights and auto-completes so i know its connecting to the right object.

when i type in db.query() the ide doesn't recognise the query method? i even imported it manually from sqlalchemy.orm and it still isn't recognised?? everything else works fine, just testing the orm via the query method is bugging.

Error says : File "C:****/**/***lib\site-packages\sqlalchemy\orm\session.py", line 747, in _connection_for_bind conn = bind.connect() AttributeError: 'function' object has no attribute 'connect'

In the tutorial the teacher just types db. query(models.Post).all() and all the functions and methods are recognized. On mine query isn't recognized. Help please.


from logging import exception
from random import randrange
from tkinter.tix import STATUS
from typing import Optional
from urllib import response
from fastapi import Body, FastAPI, Query, Response ,status, HTTPException , Depends
from pydantic import BaseModel
import psycopg2
from psycopg2.extras import RealDictCursor
import time
from sqlalchemy import create_engine, engine_from_config 
from sqlalchemy.ext.declarative import declarative_base 
from sqlalchemy.orm import sessionmaker,session , query

import models 
from database import ormengine , SessionLocal , get_db


@app.get("/sqlalchemy")
def test_post(db: session= Depends(get_db)):
    post = db.query(models.Post).all()
    return {"data":post} 

models.py file is:

import string
from typing import Text
from xmlrpc.client import boolean
from database import Base, SessionLocal
from sqlalchemy import TIMESTAMP, Integer, PrimaryKeyConstraint, String, Boolean, Column 
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql.expression import null , text
from sqlalchemy.sql.sqltypes import Text , TIMESTAMP

class Post(Base):
    __tablename__= 'apiproj2'

    id= Column( Integer , primary_key= True, nullable= False)
    title= Column( String, nullable= False)
    content = Column( String, nullable= False)
    published= Column(Boolean, server_default= 'True' , nullable=False)
    created_at = Column(TIMESTAMP( timezone=True), nullable= False , server_default= text('now()'))

database.py

from sqlalchemy import create_engine, engine_from_config
from sqlalchemy.ext.declarative import declarative_base 
from sqlalchemy.orm import sessionmaker 




 SQLALCHEMY_DATABASE_URL = "postgresql://postgres:naijalife@localhost/fastapi database"
    sqlalchemy_conn= 'postgresql://postgres:naija4life@localhost/fastapi database'
    ormengine= create_engine(sqlalchemy_conn)
    SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind= engine_from_config )
    Base = declarative_base()

# Dependency
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source