'using sqalchemy to create foreign keys on tables in dynamically created schemas
Im having trouble to create foreign keys referencing tables in a programmatically created schemas (schema name passed via the class constructor). The schema names cannot be 'hard coded' but will be made using id's in the public schema. The foreign keys referencing the public schema using object works fine though (see the 'user_id' column), but I cannot use object to declare the dynamically created schema tables.
When I try to create the table using the Create_Table2 function below, I get this error:
"Foreign key associated with column 'table2.table1_id' could not find table 'dynamic_schema_1.table1' with which to generate a foreign key to target column 'id'"
Anybody know how to work around this?
class table2():
_sSchemaName = ""
__tablename__ = "table2"
def __init__(self, sSchemaName):
self._sSchemaName = sSchemaName
def Create_Table2(self):
self._myMeta = MetaData(schema = self._sSchemaName)
sForeignKey = self._sSchemaName + ".table1.id"
self._myTable = Table(self.__tablename__, self._myMeta
, Column('id', Integer, primary_key = True)
, Column('other_table_id', Integer, db.ForeignKey(sForeignKey), nullable=False)
, Column('user_id', Integer, db.ForeignKey(User.id), nullable=False)
, db.UniqueConstraint('rel_path_id', 'file_name')
)
self._myMeta.create_all(db.engine)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
