函数对象没有属性 'connect'
function object has no attribute 'connect'
我的 sql 炼金术连接似乎有问题,不明白为什么?它有时有效,但下一次无效。另外 vs code 不会自动推荐 sql alchemy 方法,也许我设置错了?
--database.py 文件
from sqlalchemy import create_engine, engine_from_config
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
sqlalchemy_conn= 'postgresql://postgres:naija4life@localhost/fastapi database'
ormengine= create_engine(sqlalchemy_conn)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind= ormengine )
Base = declarative_base()
# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
主文件
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
import sqlalchemy
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= FastAPI()
models.Base.metadata.create_all(bind=ormengine)
while True:
try:
conn = psycopg2.connect(dbname= 'fastapi database',
host= 'localhost' , port= 5432 ,
user = 'postgres', password= 'naija4life' )
cur = conn.cursor()
print('database connection succesful')
break
except Exception as error :
print("connection failed")
print("error explanation:", error)
time.sleep(2)
class Post(BaseModel):
title: str
content: str
Published: bool = True
rating: Optional[int] = None
@app.get("/sqlalchemy")
def test_post(db: session= Depends(get_db)):
post= db.query(models.Post).all()
return {"data":post}
我得到的错误:
return await future
File "C:\Users\aobin\OneDrive\Documents\API proj\fast\lib\site-packages\anyio\_backends\_asyncio.py", line 754, in run
result = context.run(func, *args)
File "C:\Users\aobin\OneDrive\Documents\API proj\app\.\main.py", line 78, in test_post
post= db.query(models.Post).all()
File "C:\Users\aobin\OneDrive\Documents\API proj\fast\lib\site-packages\sqlalchemy\orm\query.py", line
2759, in all
return self._iter().all()
File "C:\Users\aobin\OneDrive\Documents\API proj\fast\lib\site-packages\sqlalchemy\orm\query.py", line
2894, in _iter
result = self.session.execute(
File "C:\Users\aobin\OneDrive\Documents\API proj\fast\lib\site-packages\sqlalchemy\orm\session.py", line 1691, in execute
conn = self._connection_for_bind(bind)
File "C:\Users\aobin\OneDrive\Documents\API proj\fast\lib\site-packages\sqlalchemy\orm\session.py", line 1532, in _connection_for_bind
return self._transaction._connection_for_bind(
File "C:\Users\aobin\OneDrive\Documents\API proj\fast\lib\site-packages\sqlalchemy\orm\session.py", line 747, in _connection_for_bind
conn = bind.connect()
AttributeError: 'function' object has no attribute 'connect'
在我看来,sql alchemy 的查询方法由于某种原因无法正常工作。它不会在 vs code 上自动完成either.I已经尝试了其他变体:sql_expresson,
我正在浏览 SQL 炼金术文档。引擎连接 url 位于 double-quotes 那里……所以我在我的文档中更改了它……它起作用了。 -.-'''
sqlalchemy_conn='postgresql://postgres:naija4life@localhost/fastapi database'
成为
sqlalchemy_conn= "postgresql://postgres:naija4life@localhost/fastapi 数据库"
我的 sql 炼金术连接似乎有问题,不明白为什么?它有时有效,但下一次无效。另外 vs code 不会自动推荐 sql alchemy 方法,也许我设置错了?
--database.py 文件
from sqlalchemy import create_engine, engine_from_config
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
sqlalchemy_conn= 'postgresql://postgres:naija4life@localhost/fastapi database'
ormengine= create_engine(sqlalchemy_conn)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind= ormengine )
Base = declarative_base()
# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
主文件
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
import sqlalchemy
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= FastAPI()
models.Base.metadata.create_all(bind=ormengine)
while True:
try:
conn = psycopg2.connect(dbname= 'fastapi database',
host= 'localhost' , port= 5432 ,
user = 'postgres', password= 'naija4life' )
cur = conn.cursor()
print('database connection succesful')
break
except Exception as error :
print("connection failed")
print("error explanation:", error)
time.sleep(2)
class Post(BaseModel):
title: str
content: str
Published: bool = True
rating: Optional[int] = None
@app.get("/sqlalchemy")
def test_post(db: session= Depends(get_db)):
post= db.query(models.Post).all()
return {"data":post}
我得到的错误:
return await future
File "C:\Users\aobin\OneDrive\Documents\API proj\fast\lib\site-packages\anyio\_backends\_asyncio.py", line 754, in run
result = context.run(func, *args)
File "C:\Users\aobin\OneDrive\Documents\API proj\app\.\main.py", line 78, in test_post
post= db.query(models.Post).all()
File "C:\Users\aobin\OneDrive\Documents\API proj\fast\lib\site-packages\sqlalchemy\orm\query.py", line
2759, in all
return self._iter().all()
File "C:\Users\aobin\OneDrive\Documents\API proj\fast\lib\site-packages\sqlalchemy\orm\query.py", line
2894, in _iter
result = self.session.execute(
File "C:\Users\aobin\OneDrive\Documents\API proj\fast\lib\site-packages\sqlalchemy\orm\session.py", line 1691, in execute
conn = self._connection_for_bind(bind)
File "C:\Users\aobin\OneDrive\Documents\API proj\fast\lib\site-packages\sqlalchemy\orm\session.py", line 1532, in _connection_for_bind
return self._transaction._connection_for_bind(
File "C:\Users\aobin\OneDrive\Documents\API proj\fast\lib\site-packages\sqlalchemy\orm\session.py", line 747, in _connection_for_bind
conn = bind.connect()
AttributeError: 'function' object has no attribute 'connect'
在我看来,sql alchemy 的查询方法由于某种原因无法正常工作。它不会在 vs code 上自动完成either.I已经尝试了其他变体:sql_expresson,
我正在浏览 SQL 炼金术文档。引擎连接 url 位于 double-quotes 那里……所以我在我的文档中更改了它……它起作用了。 -.-'''
sqlalchemy_conn='postgresql://postgres:naija4life@localhost/fastapi database' 成为 sqlalchemy_conn= "postgresql://postgres:naija4life@localhost/fastapi 数据库"