Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

370
Views
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()
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I was calling the wrong engine, I should've called ormengine. The engine I created to connect to the database. The tutorial had an error in it. I changed this but it didn't work, after restarting vs code it worked. Thanks for the help guys.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!