Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To combine two tables from two separate MSSQL databases in Fastapi and SqlAlchemy, you can use the following method:

  1. Create two SqlAlchemy engine objects with connection strings for each database.

  2. Use the .table_names() method of each engine object to confirm the names of the tables in their respective databases.

  3. Use the .reflect() method on each engine object to read all the table metadata for each database.

  4. Create two SqlAlchemy metadata objects, and use the .merge() method to combine the table metadata for both databases.

  5. Use the .session() method of the combined metadata object to create a new session.

  6. Use the .query() method on this session to extract data from both tables as needed.

  7. You can use JOIN queries or Union queries to join data from both tables.

The resulting combined data can then be used to generate responses for FastApi endpoints.