def define_constraint_remote_table(self, constraint, table, preparer):
"""Format the remote table clause of a CREATE CONSTRAINT clause.
If using InnoDB, tables without manually-provided schemas will
be formatted using the default connection schema when rendered
in foreign key statements. This is because InnoDB (or perhaps
mysql?) will interpret references without schemas as being in the
same schema as the table being created/altered """
is_innodb = engine_key in table.kwargs and \
table.kwargs[engine_key].lower() == 'innodb'
if is_innodb and table.schema is None:
default_schema = table.bind.url.database
constraint_schema = constraint.columns[0].table.schema
if constraint_schema not in (default_schema, None):
""" if the constraint schema is not the default, we need to
add a schema before formatting the table """
table.schema = default_schema
value = preparer.format_table(table)
table.schema = None
return value
return preparer.format_table(table)
This does exactly what I want it to, and ensures that in any cases where the referenced keys are in a different schema than the active table, the keys are prefixed by the schema.
Would this version of the formatting function not be more appropriate than the current, as the current is unable to handle a simple 2 model relation when they are in different schemas?