tortoise-orm icon indicating copy to clipboard operation
tortoise-orm copied to clipboard

AttributeError: 'Query' object has no attribute 'where' exectutor.py line 92

Open nihilok opened this issue 5 years ago • 6 comments

Describe the bug Can't create new db entries. I've tried with a fresh database which is no different. Leaving this here for a while in case it is a bug. I can't work out the error message, but I've started from scratch with the api in the hope of not encountering it again.

To Reproduce At the moment it's very easy for me to reproduce this issue, but I think it might be related to the way my FastAPI app / models have been set up, because the .create(**kwargs) method works in other areas of the app.

The following FastAPI route causes the error:

@router.post('/orders/')
async def create_new_order(item_id: int, user: CustomerP = Depends(get_current_user_from_hash)):
    print(user.id)
    print(item_id)
    await Test.create()

It also causes the error if I try to create any other model, but the print statements both execute, and then the error hits on the await Test.create() line.

Expected behavior I expect the model to create an entry in the database like it does in other areas of my app.

Additional context cv_server/api/__init__.py

from .shopify_app.routes import router as commerce_router
from .anon_auth.access_token import router as auth_router

app = FastAPI()
app.include_router(auth_router)
app.include_router(commerce_router)

...
register_tortoise(
    app,
    db_url=f'sqlite://curious.sqlite3',
    modules={'models': ["cv_server.api.db.models"]},
    generate_schemas=True,
    add_exception_handlers=True
)

cv_server/api/db/models/__init__.py

from tortoise import Tortoise

from .customer import Customer, Order, OrderFinal
from .product import Product, Tag
from .test import Test

Tortoise.init_models(["cv_server.api.db.models"], "models")

The Test model looks like this:

from tortoise import Model, fields

class Test(Model):
    id = fields.BigIntField(pk=True)

Full traceback:

2
1
INFO:     127.0.0.1:48090 - "POST /orders/?item_id=1&token=secrASDSDFGDGettoken.eyJzdWIiOiJhbm9uIiwiaGFzaCI6ImInXFx4MWFYbFxceDg5QS47XFx4YWQrM0NcXHhjMFxceGRlQVliJyIsImV4cCI6MTYyNzQ3ODE3NH0.65pUZe5X-s9EuzRs3OAit90z2S3tYeYr-1kNGGS6Sx4 HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 369, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 59, in __call__
    return await self.app(scope, receive, send)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/fastapi/applications.py", line 199, in __call__
    await super().__call__(scope, receive, send)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc from None
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/middleware/cors.py", line 86, in __call__
    await self.simple_response(scope, receive, send, request_headers=headers)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/middleware/cors.py", line 142, in simple_response
    await self.app(scope, receive, send)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc from None
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/routing.py", line 580, in __call__
    await route.handle(scope, receive, send)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/routing.py", line 241, in handle
    await self.app(scope, receive, send)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/routing.py", line 52, in app
    response = await func(request)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/fastapi/routing.py", line 216, in app
    raw_response = await run_endpoint_function(
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/fastapi/routing.py", line 149, in run_endpoint_function
    return await dependant.call(**values)
  File "/home/mj/Freelance/curious-vintage-pwa/cv_server/api/shopify_app/routes.py", line 66, in create_new_order
    await Customer.create()
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/tortoise/models.py", line 1105, in create
    await instance.save(using_db=db, force_create=True)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/tortoise/models.py", line 907, in save
    executor = db.executor_class(model=self.__class__, db=db)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/tortoise/backends/base/executor.py", line 92, in __init__
    self.model._meta.basequery.where(
AttributeError: 'Query' object has no attribute 'where'
ERROR:uvicorn.error:Exception in ASGI application
Traceback (most recent call last):
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 369, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 59, in __call__
    return await self.app(scope, receive, send)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/fastapi/applications.py", line 199, in __call__
    await super().__call__(scope, receive, send)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc from None
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/middleware/cors.py", line 86, in __call__
    await self.simple_response(scope, receive, send, request_headers=headers)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/middleware/cors.py", line 142, in simple_response
    await self.app(scope, receive, send)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc from None
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/routing.py", line 580, in __call__
    await route.handle(scope, receive, send)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/routing.py", line 241, in handle
    await self.app(scope, receive, send)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/starlette/routing.py", line 52, in app
    response = await func(request)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/fastapi/routing.py", line 216, in app
    raw_response = await run_endpoint_function(
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/fastapi/routing.py", line 149, in run_endpoint_function
    return await dependant.call(**values)
  File "/home/mj/Freelance/curious-vintage-pwa/cv_server/api/shopify_app/routes.py", line 66, in create_new_order
    await Customer.create()
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/tortoise/models.py", line 1105, in create
    await instance.save(using_db=db, force_create=True)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/tortoise/models.py", line 907, in save
    executor = db.executor_class(model=self.__class__, db=db)
  File "/home/mj/Freelance/RetroMonkey/retro-monkey-trading/curious_vintage_api/venv/lib/python3.9/site-packages/tortoise/backends/base/executor.py", line 92, in __init__
    self.model._meta.basequery.where(
AttributeError: 'Query' object has no attribute 'where'

nihilok avatar Jul 23 '21 13:07 nihilok

See https://docs.sqlalchemy.org/en/14/orm/query.html?highlight=where#sqlalchemy.orm.Query.where

You either need to update to SQLAlchemy v1.4, or change this:

self.model._meta.basequery.where(

to:

self.model._meta.basequery.filter(

(Commenting because I just ran into the same mistake, and this issue came up via a Google search)

peterjc avatar Jul 28 '21 13:07 peterjc

Me too, I'm looking for a cause, too. I guess there's a problem with the model declaration. If you have solved it, please let me know the answer.

hyeonprojects avatar Aug 02 '21 05:08 hyeonprojects

have you fixed that, facing the same issue

Reszkettle avatar Aug 06 '21 21:08 Reszkettle

okay for me it was wrong modules path in register_tortoise

Reszkettle avatar Aug 06 '21 21:08 Reszkettle

@Reszkettle Oh, I see my module path..... Thanks

hyeonprojects avatar Aug 12 '21 11:08 hyeonprojects

Thanks @Reszkettle . Solved using your comment, changing main.py to myfilename.py

register_tortoise(
    ...
    modules={'models': [ myfilename.py ]},
    ...
)

mathphysmx avatar Jan 04 '22 00:01 mathphysmx