Я пытаюсь отразить представление MS SQL Server в таблице SQLite, используя Python SQLAlchemy.
Проблема состоит в том, что sqlalchemy добавляет COLLATE "SQL_Latin1_General_CP1_CI_AS" к различным столбцам NVARCHAR, что не поддерживается в sqlite.
Существует ли независимый от имени столбца способ удаления всех COLLATES из определения таблицы?
from sqlalchemy import *
from sqlalchemy.orm import create_session
from sqlalchemy.ext.declarative import declarative_base
import urllib
from sqlalchemy.orm import sessionmaker
#Create and engine and get the metadata
Base = declarative_base()
source_connection = 'msql+pyodbc...'
source_engine = create_engine(source_connection)
metadata = MetaData(bind=source_engine)
SourceSession = sessionmaker(source_engine)
#destination
dest_engine = create_engine('sqlite:///...', echo=True)
DestSession = sessionmaker(dest_engine)
#Reflect each database table we need to use, using metadata
class tblR(Base):
__table__ = Table('tblR', metadata,
Column("r_id", Integer, primary_key=True),
autoload=True)
#Create a session to use the tables
# This is the query we want to persist in a new table:
sourceSession = SourceSession()
query= sourceSession.query(tblR.r_id, tblR.MK_Assumed).filter_by(r_id=0)
# Build the schema for the new table
# based on the columns that will be returned
# by the query:
metadata = MetaData(bind=dest_engine)
columns = [Column(desc['name'], desc['type']) for desc in query.column_descriptions]
column_names = [desc['name'] for desc in query.column_descriptions]
table = Table("newtable", metadata, *columns)
# Create the new table in the destination database
table.create(dest_engine)
# Finally execute the query
destSession = DestSession()
for row in query:
destSession.execute(table.insert(row))
destSession.commit()
Я получаю следующую ошибку:
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such collation sequence: SQL_Latin1_General_CP1_CI_AS [SQL: '\nCREATE TABLE newtable (\n\tr_id INTEGER, \n\t"MK_Assumed" NVARCHAR(20) COLLATE "SQL_Latin1_General_CP1_CI_AS"\n)\n\n'] (Background on this error at: http://sqlalche.me/e/e3q8)