Existing PostgreSQL database have tables organized in different \"schemas\" to split a large database (both for scaling and implementing fine-tuned security at server level). Si
I finally found the error - a typo or extra underscore in table beta declaration: instead of the correct __tablename__
I had __table_name__
. Without the __tablename__
class member specified, SQLAlchemy will create the table on the server using the class name as the table name (default behavior). In my case, it created table TableBeta instead of intended beta. That caused the foreign key mixin to not find the table.
The approach I took (detailed in my question) is the correct way of using mixins and specifying schema in SQLAlchemy for the use case (tables in different schemas and table class declarations in different model files). Schema name is specified in table declarations using the __table_args__
class member passed with a keyword dictionary {"schema": "schema_name"}
. Schema name is qualified in mixin/column declarations in the form "schema_name.table_name.column_name"
.