I have a SQLAlchemy class like so:
class TableName(Base):
__table_args__ = (
Index('index_name', 'column_name'),
)
I want the name of the index to compile differently depending on the dialect, because as far as I understand it, MySQL can compile indexes with the same name as long as they are in different tables, whereas Sqlite requires every index to have a unique name. We have some tables that use the same index name for mysql, and I want it to compile to sqlite for testing. Is this possible or am I confused for some other reason?
CodePudding user response:
I needed to compile the CreateIndex
function, and use the index and table names that were passed in to create a unique index name:
@compiles(CreateIndex, "sqlite")
def compile_index(element, compiler, **kw):
index_name = element.element.name
table_name = element.element.table.name
column_name = element.element.expressions[0].name
unique_index_name = f"{table_name} {index_name}"
return "CREATE INDEX \"%s\" ON %s (\"%s\")" % (unique_index_name, table_name, column_name)