Using a different schema for the same declarative Base in sqlalchemy

泪湿孤枕 提交于 2019-12-02 23:49:19

I think you need a different model for each schema. __abstract__ can make this less painful. This follows on to Paul Yin's answer...

  1. Define an __abstract__ LoadTender model, so you don't have to keep coding it.

    #base.py
    class LoadTender(Base):
        __abstract__ = True
        id = ...
        def __repr__ ...
    
  2. Put a schema-specific Base in the hierarchy for each schema.

    #schema1.py
    from base import LoadTender
    
    PublicBase = declarative_base(metadata=MetaData(schema='public'))
    
    class LoadTender(PublicBase, LoadTender):
        __tablename__ = 'load_tenders'
    
  3. Do the same for other schema.

You can have a base module in package model

app\
    models\
        base.py
        schema1.py
        schema2.py
    views\
    ...

declare Base in base.py, then import it to other schemas

Tom Willis

just a guess

LoadTender.__table_args__["schema"] = "whatever"

Probably best to put it somewhere where your app configurator is creating the app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!