ponyorm / pony

Pony Object Relational Mapper
Apache License 2.0
3.62k stars 244 forks source link

'function' object has no attribute 'select' #522

Open exqmjmz opened 4 years ago

exqmjmz commented 4 years ago

Error

Traceback (most recent call last):
  File "D:\项目\fastAPI-comment\venv\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 384, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "D:\项目\fastAPI-comment\venv\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "D:\项目\fastAPI-comment\venv\lib\site-packages\fastapi\applications.py", line 165, in __call__
    await super().__call__(scope, receive, send)
  File "D:\项目\fastAPI-comment\venv\lib\site-packages\starlette\applications.py", line 102, in __call__
    await self.middleware_stack(scope, receive, send)
  File "D:\项目\fastAPI-comment\venv\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
    raise exc from None
  File "D:\项目\fastAPI-comment\venv\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "D:\项目\fastAPI-comment\venv\lib\site-packages\starlette\exceptions.py", line 82, in __call__
    raise exc from None
  File "D:\项目\fastAPI-comment\venv\lib\site-packages\starlette\exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "D:\项目\fastAPI-comment\venv\lib\site-packages\starlette\routing.py", line 550, in __call__
    await route.handle(scope, receive, send)
  File "D:\项目\fastAPI-comment\venv\lib\site-packages\starlette\routing.py", line 227, in handle
    await self.app(scope, receive, send)
  File "D:\项目\fastAPI-comment\venv\lib\site-packages\starlette\routing.py", line 41, in app
    response = await func(request)
  File "D:\项目\fastAPI-comment\venv\lib\site-packages\fastapi\routing.py", line 196, in app
    raw_response = await run_endpoint_function(
  File "D:\项目\fastAPI-comment\venv\lib\site-packages\fastapi\routing.py", line 150, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "D:\项目\fastAPI-comment\venv\lib\site-packages\starlette\concurrency.py", line 34, in run_in_threadpool
    return await loop.run_in_executor(None, func, *args)
  File "c:\program files\python38\lib\concurrent\futures\thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "d:\项目\fastAPI-comment\api\comment.py", line 11, in get_AllComment
    allCommentJson = json.loads(dbSchemes.inquire_AllComment())
  File "<string>", line 2, in inquire_AllComment
  File "D:\项目\fastAPI-comment\venv\lib\site-packages\pony\orm\core.py", line 528, in new_func
    result = func(*args, **kwargs)
  File "d:\项目\fastAPI-comment\database\schemes.py", line 6, in inquire_AllComment
    commentList = Comment.select(lambda c: c.article != None)[:]
AttributeError: 'function' object has no attribute 'select'

Code

import json
from .model import *

@db_session
def inquire_AllComment():
    commentList = Comment.select(lambda c: c.article != None)[:]
    return json.dumps(commentList)

@db_session
def insert_CommentArticle(comment):
    commentDict = json.loads(comment)
    Comment(
        userName = commentDict['userName'],
        mail = commentDict['mail'],
        article = commentDict['article'],
        createTime = commentDict['createTime']
    )
    return '成功'

Model

from pony.orm import *

db = Database()
db.bind(provider='sqlite',filename='database.db',create_db=True)

@db_session
class Comment(db.Entity):
    id = PrimaryKey(int, auto=True)
    userName = Required(str)
    mail = Optional(str)
    article = Required(str)
    createTime = Required(str)

@db_session
class UserInfo(db.Entity):
    id = PrimaryKey(int, auto=True)
    userName = Required(str)
    comments = set('Comment')

db.generate_mapping(create_tables=True)
andreymal commented 4 years ago

Remove @db_session from models