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

ConfigurationError: No DB associated to model

Open vivekjoshy opened this issue 3 years ago • 3 comments

I'm getting this error when I try to execute any path on my API:

2021-08-03 22:08:41.031 | INFO     | uvicorn.protocols.http.h11_impl:send:434 - 127.0.0.1:64114 - "GET /users HTTP/1.1" 500
2021-08-03 22:08:41.031 | ERROR    | uvicorn.protocols.http.h11_impl:run_asgi:372 - Exception in ASGI application

Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File "C:\Users\taven\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
               │     │   └ 280
               │     └ 3
               └ <function _main at 0x00000132C644B5E0>

  File "C:\Users\taven\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 129, in _main
    return self._bootstrap(parent_sentinel)
           │    │          └ 280
           │    └ <function BaseProcess._bootstrap at 0x00000132C63AB940>
           └ <SpawnProcess name='SpawnProcess-1' parent=13876 started>

  File "C:\Users\taven\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py", line 315, in _bootstrap
    self.run()
    │    └ <function BaseProcess.run at 0x00000132C63A6F70>
    └ <SpawnProcess name='SpawnProcess-1' parent=13876 started>

  File "C:\Users\taven\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
    │    │        │    │        │    └ {'config': <uvicorn.config.Config object at 0x00000132C6360610>, 'target': <bound method Server.run of <uvicorn.server.Server...
    │    │        │    │        └ <SpawnProcess name='SpawnProcess-1' parent=13876 started>
    │    │        │    └ ()
    │    │        └ <SpawnProcess name='SpawnProcess-1' parent=13876 started>
    │    └ <function subprocess_started at 0x00000132C8715D30>
    └ <SpawnProcess name='SpawnProcess-1' parent=13876 started>

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\uvicorn\subprocess.py", line 76, in subprocess_started
    target(sockets=sockets)
    │              └ [<socket.socket fd=704, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 5000)>]
    └ <bound method Server.run of <uvicorn.server.Server object at 0x00000132C6360B20>>

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\uvicorn\server.py", line 50, in run
    loop.run_until_complete(self.serve(sockets=sockets))
    │    │                  │    │             └ [<socket.socket fd=704, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 5000)>]
    │    │                  │    └ <function Server.serve at 0x00000132C87153A0>
    │    │                  └ <uvicorn.server.Server object at 0x00000132C6360B20>
    │    └ <function BaseEventLoop.run_until_complete at 0x00000132C7FD8550>
    └ <_WindowsSelectorEventLoop running=True closed=False debug=False>

  File "C:\Users\taven\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 629, in run_until_complete
    self.run_forever()
    │    └ <function BaseEventLoop.run_forever at 0x00000132C7FD84C0>
    └ <_WindowsSelectorEventLoop running=True closed=False debug=False>

  File "C:\Users\taven\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 596, in run_forever
    self._run_once()
    │    └ <function BaseEventLoop._run_once at 0x00000132C7FDA040>
    └ <_WindowsSelectorEventLoop running=True closed=False debug=False>

  File "C:\Users\taven\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 1890, in _run_once
    handle._run()
    │      └ <function Handle._run at 0x00000132C7F67C10>
    └ <Handle <TaskStepMethWrapper object at 0x00000132C98DD100>()>

  File "C:\Users\taven\AppData\Local\Programs\Python\Python39\lib\asyncio\events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
    │    │            │    │           │    └ <member '_args' of 'Handle' objects>
    │    │            │    │           └ <Handle <TaskStepMethWrapper object at 0x00000132C98DD100>()>
    │    │            │    └ <member '_callback' of 'Handle' objects>
    │    │            └ <Handle <TaskStepMethWrapper object at 0x00000132C98DD100>()>
    │    └ <member '_context' of 'Handle' objects>
    └ <Handle <TaskStepMethWrapper object at 0x00000132C98DD100>()>

> File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 369, in run_asgi
    result = await app(self.scope, self.receive, self.send)
                   │   │    │      │    │        │    └ <function RequestResponseCycle.send at 0x00000132C87F73A0>
                   │   │    │      │    │        └ <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000132C98DD070>
                   │   │    │      │    └ <function RequestResponseCycle.receive at 0x00000132C87F7430>
                   │   │    │      └ <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000132C98DD070>
                   │   │    └ {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('127.0.0.1', 5000), 'cl...
                   │   └ <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000132C98DD070>
                   └ <uvicorn.middleware.proxy_headers.ProxyHeadersMiddleware object at 0x00000132C87B35E0>

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 59, in __call__
    return await self.app(scope, receive, send)
                 │    │   │      │        └ <bound method RequestResponseCycle.send of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000132C98DD070>>
                 │    │   │      └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000132C98DD...
                 │    │   └ {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('127.0.0.1', 5000), 'cl...
                 │    └ <uvicorn.middleware.debug.DebugMiddleware object at 0x00000132C87B3550>
                 └ <uvicorn.middleware.proxy_headers.ProxyHeadersMiddleware object at 0x00000132C87B35E0>

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\uvicorn\middleware\debug.py", line 96, in __call__
    raise exc from None

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\uvicorn\middleware\debug.py", line 93, in __call__
    await self.app(scope, receive, inner_send)
          │    │   │      │        └ <function DebugMiddleware.__call__.<locals>.inner_send at 0x00000132C9890C10>
          │    │   │      └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000132C98DD...
          │    │   └ {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('127.0.0.1', 5000), 'cl...
          │    └ <fastapi.applications.FastAPI object at 0x00000132C8F57B20>
          └ <uvicorn.middleware.debug.DebugMiddleware object at 0x00000132C87B3550>

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\fastapi\applications.py", line 199, in __call__
    await super().__call__(scope, receive, send)
                           │      │        └ <function DebugMiddleware.__call__.<locals>.inner_send at 0x00000132C9890C10>
                           │      └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000132C98DD...
                           └ {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('127.0.0.1', 5000), 'cl...

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\starlette\applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
          │    │                │      │        └ <function DebugMiddleware.__call__.<locals>.inner_send at 0x00000132C9890C10>
          │    │                │      └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000132C98DD...
          │    │                └ {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('127.0.0.1', 5000), 'cl...
          │    └ <starlette.middleware.errors.ServerErrorMiddleware object at 0x00000132C9887040>
          └ <fastapi.applications.FastAPI object at 0x00000132C8F57B20>

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
    raise exc from None

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
          │    │   │      │        └ <function ServerErrorMiddleware.__call__.<locals>._send at 0x00000132C98909D0>
          │    │   │      └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000132C98DD...
          │    │   └ {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('127.0.0.1', 5000), 'cl...
          │    └ <starlette.exceptions.ExceptionMiddleware object at 0x00000132C9876D90>
          └ <starlette.middleware.errors.ServerErrorMiddleware object at 0x00000132C9887040>

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\starlette\exceptions.py", line 82, in __call__
    raise exc from None

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\starlette\exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
          │    │   │      │        └ <function ExceptionMiddleware.__call__.<locals>.sender at 0x00000132C9890CA0>
          │    │   │      └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000132C98DD...
          │    │   └ {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('127.0.0.1', 5000), 'cl...
          │    └ <fastapi.routing.APIRouter object at 0x00000132C8F576D0>
          └ <starlette.exceptions.ExceptionMiddleware object at 0x00000132C9876D90>

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\starlette\routing.py", line 580, in __call__
    await route.handle(scope, receive, send)
          │     │      │      │        └ <function ExceptionMiddleware.__call__.<locals>.sender at 0x00000132C9890CA0>
          │     │      │      └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000132C98DD...
          │     │      └ {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('127.0.0.1', 5000), 'cl...
          │     └ <function Route.handle at 0x00000132C8C93040>
          └ <fastapi.routing.APIRoute object at 0x00000132C9887280>

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\starlette\routing.py", line 241, in handle
    await self.app(scope, receive, send)
          │    │   │      │        └ <function ExceptionMiddleware.__call__.<locals>.sender at 0x00000132C9890CA0>
          │    │   │      └ <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x00000132C98DD...
          │    │   └ {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('127.0.0.1', 5000), 'cl...
          │    └ <function request_response.<locals>.app at 0x00000132C9890040>
          └ <fastapi.routing.APIRoute object at 0x00000132C9887280>

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\starlette\routing.py", line 52, in app
    response = await func(request)
                     │    └ <starlette.requests.Request object at 0x00000132C8FCF040>
                     └ <function get_request_handler.<locals>.app at 0x00000132C8F75CA0>

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\fastapi\routing.py", line 216, in app
    raw_response = await run_endpoint_function(
                         └ <function run_endpoint_function at 0x00000132C8C890D0>

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\fastapi\routing.py", line 149, in run_endpoint_function
    return await dependant.call(**values)
                 │         │      └ {}
                 │         └ <function get_users at 0x00000132C8F4F790>
                 └ <fastapi.dependencies.models.Dependant object at 0x00000132C9887730>

  File "C:\Users\taven\PycharmProjects\DebateWiki\DebateWiki\versions\v1\users.py", line 14, in get_users
    return await User_Pydantic.from_queryset(Users.all())
                 │             │             │     └ <classmethod object at 0x00000132C8F08F10>
                 │             │             └ <class 'DebateWiki.models.Users'>
                 │             └ <classmethod object at 0x00000132C8F554C0>
                 └ <class 'abc.User'>

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\tortoise\models.py", line 1183, in all
    return cls._meta.manager.get_queryset()
           │   │     └ <member 'manager' of 'MetaInfo' objects>
           │   └ <tortoise.models.MetaInfo object at 0x00000132C8EAC900>
           └ <class 'DebateWiki.models.Users'>

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\tortoise\manager.py", line 15, in get_queryset
    return QuerySet(self._model)
           │        │    └ <class 'DebateWiki.models.Users'>
           │        └ <tortoise.manager.Manager object at 0x00000132C8F570D0>
           └ <class 'tortoise.queryset.QuerySet'>

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\tortoise\queryset.py", line 293, in __init__
    super().__init__(model)
                     └ <class 'DebateWiki.models.Users'>

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\tortoise\queryset.py", line 93, in __init__
    self.capabilities: Capabilities = model._meta.db.capabilities
    │    │                            │     │     └ <property object at 0x00000132C8F00E00>
    │    │                            │     └ <tortoise.models.MetaInfo object at 0x00000132C8EAC900>
    │    │                            └ <class 'DebateWiki.models.Users'>
    │    └ <member 'capabilities' of 'AwaitableQuery' objects>
    └ <tortoise.queryset.QuerySet object at 0x00000132C8F63370>

  File "C:\Users\taven\PycharmProjects\DebateWiki\venv\3.9\lib\site-packages\tortoise\models.py", line 276, in db
    raise ConfigurationError("No DB associated to model")
          └ <class 'tortoise.exceptions.ConfigurationError'>

tortoise.exceptions.ConfigurationError: No DB associated to model

How do I solve this issue? I'm using PostgreSQL.

My register function: Source

vivekjoshy avatar Aug 03 '21 16:08 vivekjoshy

I'm having the same issue trying to run tests using the initializer() function, passing in a list with modules that contain my models.

Fingel avatar Aug 03 '21 17:08 Fingel

I have now managed to narrow it down to a problem with uvicorn (maybe?). Using the import string (or workers) with uvicorn programmatically doesn't seem to work with tortoise. Instead simply passing the app instance directly to uvicorn.run() works.

vivekjoshy avatar Aug 04 '21 08:08 vivekjoshy

This seems to happen because initializer doesn't support mulitple apps. I made a custom version of it and it worked. Might open an issue on this later today.

primal100 avatar Nov 30 '23 11:11 primal100