How to specify PostgreSQL schema in SQLAlchemy column/foreign key mixin?

后端 未结 1 1178
孤独总比滥情好
孤独总比滥情好 2021-02-08 15:16

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

1条回答
  •  梦毁少年i
    2021-02-08 15:32

    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".

    0 讨论(0)
提交回复
热议问题