Closed davidmcnabnz closed 4 months ago
Hi
There is example how you can use two databases https://github.com/tortoise/tortoise-orm/blob/develop/examples/two_databases.py
Is not what you are looking for?
Please write if I misunderstood your request
Thanks, but the example you've given uses .execute_query()
, instead of being able to use the ORM methods such as Event.filter( ... )
.
Can you recommend how to have one models module for a database on one server, and another models module for a database on a different server, and to access both through Tortoise ORM patterns?
@davidmcnabnz All filter(...)
method and other method should work fine. You can try to copy this example locally, modify it and see that it works.
Only real limitation here - is that, obviously, you can't cross-reference models from different databases
Explicit . execute_query
calls in these example is only needed to verify that Event
model is running on second connection, but failing on first connection
Is your feature request related to a problem? Please describe.
I need to be able to access multiple databases simultaneously, on on local machine, and 2 or more on other remote servers. But Tortoise's usage pattern suggests it's only meant for accessing one database within the one Python environment.
Describe the solution you'd like
Here's the OLD way - singleton Tortoise context:
NEW preferred way - multple distinct Tortoise contexts:
This lets me then make arbitrary access to multiple databases at once:
Describe alternatives you've considered In production use, I am limited to using Tortoise for the local PostgreSQL database, and low-level asyncpg pooled connections to PostgreSQL databases on other servers. But I would greatly prefer to be able to wrap all databases, local and remote, within Tortoise's excellent ORM layers.
Additional context Add any other context about the feature request here.