I want to copy a table from SQL Server to Sqlite via SqlAlchemy reflection.
from sqlalchemy import create_engine, MetaData, Table
source_engine = create_engine(...) # create sql server engine
source_connection = source_engine.connect()
source_metadata = MetaData() # (1)
destination_engine = create_engine('sqlite:///test.db', echo=True)
destination_connection = destination_engine.connect()
Table('table_name', source_metadata, autoload=True, autoload_with=source_engine) # (2)
source_metadata.create_all(destination_engine)
The above works with the default schema (dbo) but I want to specify the schema of the reflected tables in SQL Server.
When I change (1) to source_metadata = Metadata(schema='dbo') or (2) to Table(table_name, source_metadata, autoload=True, autoload_with=source_engine, schema='dbo') I get the following error:
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unknown database "dbo"
I know the issue is with sqlite. When I create a table in SQLite Studio like below, I get the same error:
CREATE TABLE dbo.table_name(...
but the following creates the table successfully:
CREATE TABLE [dbo.table_name](...
Finally, I have tried the below with no success:
destination_engine.execute("ATTACH DATABASE 'test.db' AS 'dbo'")
How do I create a table specifying the schema using SqlAlchemy?