1

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()
C A OB1
  • 75
  • 2
  • 14
  • 1
    Likely an issue with how you're importing a dependency. See: https://stackoverflow.com/a/4534443/2479481 – Luke Willis Feb 11 '22 at 18:35
  • 1
    The error message you've included is nowhere in your code - include the code that actually produces the error. In your view you're also using `query` directly as a function and not calling it on the session. Since you haven't typed the session argument as a SessionLocal or similar, your IDE probably don't know what the actual type of the dependency returned from Depends is. – MatsLindh Feb 11 '22 at 18:56
  • @MatsLindh i've added the extra code. I have no idea why it wouldnt recognise the query function, I've even tried importing it directly and it's not being recognised? im literally following the tutorial word for word. –  C A OB1 Feb 11 '22 at 19:24
  • You never call `engine_from_config` - you just give the function as the argument. You probably want to call it - `bind=engine_from_config()`. As I've mentioned, your IDE have no reason to know what `query` is from how it has been defined. – MatsLindh Feb 11 '22 at 19:50
  • @MatsLindh I called the wrong engine, it was an error in the tutorial. I was calling engine from config instead of the engine I had created to connect to the database (orm engine). Thanks for your help mats –  C A OB1 Feb 12 '22 at 13:51

1 Answers1

1

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.

C A OB1
  • 75
  • 2
  • 14