0

I can't seem to find a good answer to this. What is the correct way in SQLAlchemy (declarative) to connect two SQLite databases? I also need to make a relation between two tables in these two seperate databases.

Edit: It is some sort of commenting system. I already have a database with data (posts) and I'm now building a social Flask webapplication around it where users can register and comment on these posts. Preferably, I would like to keep the code for the posts database as unchanged as possible so I can use the same code for the web and desktop application.

This is how it looks basicly:

database1 (created and controlled by flask-sqlalchemy):
  - users
  - comments

database2 (created and controlled by SQLAlchemy):
  - posts
Timo
  • 164
  • 2
  • 12

1 Answers1

0

Please take a look at the documentation for Simple Vertical Partitioning, quote from there:

Vertical partitioning places different kinds of objects, or different tables, across multiple databases:

engine1 = create_engine('postgresql://db1')
engine2 = create_engine('postgresql://db2')

Session = sessionmaker(twophase=True)

# bind User operations to engine 1, Account operations to engine 2
Session.configure(binds={User:engine1, Account:engine2})

session = Session()

Also read the answer inside of the question SQLAlchemy Declarative + relationships across multiple different databases, which will give you more sample code to start with

Community
  • 1
  • 1
van
  • 74,297
  • 13
  • 168
  • 171