Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

369
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda