How to use two different metadata properly to connect to two databases?
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the SQLModel documentation, with the integrated search.
- I already searched in Google “How to X in SQLModel” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to SQLModel but to Pydantic.
- I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from typing import Optional
from sqlmodel import Field, SQLModel
import sqlalchemy
metadata1 = sqlalchemy.MetaData()
metadata2 = sqlalchemy.MetaData()
# all_metadata = sqlmodel.SQLModel.metadata
class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str
secret_name: str
age: Optional[int] = None
__table_args__ = (
metadata1, # This setting has no effect !! :(
)
class Boss(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str
age: Optional[int] = None
__table_args__ = (
metadata2, # This setting has no effect !! :(
)
engine1 = sqlalchemy.create_engine("database 1")
engine2 = sqlalchemy.create_engine("database 2")
metadata1.create_all(engine1)
metadata2.create_all(engine2)
## in alembic's env.py
# target_metadata = {
# 'engine1': mymodel.metadata1,
# 'engine2': mymodel.metadata2
#}
Description
- I want to use two databases that have different table groups.
- SQLModel provides only one metadata (sqlmodel.SQLModel.metadata)
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
3.10.0
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:8
- Comments:9 (3 by maintainers)
Top Results From Across the Web
How to use SQLModel with more than 1 database?
I would like to use multiple different databases in the same project. For example I would like one FastAPI endpoint to query 1...
Read more >Manage metadata when making a database available on ...
Distributed queries access data from multiple heterogeneous data sources on either the same or different computers.
Read more >Managing database connections - Mode Support
You can connect Mode to the same database multiple times via different database users, with varying levels of permissions.
Read more >Chapter 4. Multiple Database Programming with UnityJDBC
Multiple source UnityJDBC queries can be used with an INSERT INTO statement to populate a table in the database. This allows a user...
Read more >Configuring multiple database tables as the source
Select. User Defined Join. and define the join. · Any existing relationships are added to the join condition. To ensure that you enter...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I finally found a simple way to create tables in different databases.
This seems to work, although I did not test is thoroughly: