tortoise / tortoise/tortoise-orm
Is it possible to use multiple MySQL databases/schemas on the same connection?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 516
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 9
Description
In our project we use multiple MySQL schemas/databases. Now, the only way to access the data, is to create a separate 'app' record with their own connection:
class Model1(Model):
..
class Meta:
app = 'app_for_schema1'
table = 'table1'
class Model2(Model):
..
class Meta:
app = 'app_for_schema2'
table = 'table2'
Tried to find a workaround but without success:
- If I specify schema name in the table field 'schema1.table1', then the full model's table name becomes
schema1.schema1.table1 - If I don't set db name in the connection string, then the first query fails with "no database selected" error (a lack of
USE <schema/database>call.
Question: Is it possible to use multiple db schemas on the same connection?
What if Tortoise ORM started checking the table name and if it contains '.', it wouldn't add db name as a prefix to the table name? Or, alternatively, an extra Meta field could be added, e.g. schema which would override db name specified in the connection string.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing how MySQL connection settings and model Meta app/table fields produce the final table name. Compare the current handling of schema-qualified names with the proposed schema Meta field or omitted database selection. Done would require a decided behavior for multiple schemas on one connection and validation that both proposed configurations avoid duplicated prefixes and missing-database errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mysql, python
- Domain
- database
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 32/100