flask-sqlalchemy - PostgreSQL - Define specific schema for table?

会有一股神秘感。 提交于 2019-12-20 09:53:34

问题


I want to define a specific schema for a 'model' using flask-sqlalchemy. When you create a table object in sqlalchemy itself it has a parameter to pass for schema name.

How do I do this in flask-sqlalchemy?


回答1:


When you define your model class use:

__table_args__ = {"schema":"schema_name"}

maybe it will save someone else some hunting.




回答2:


For future references:

db = flask.ext.sqlalchemy.SQLAlchemy(app)

app.config['SQLALCHEMY_DATABASE_URI'] = 'your_default_schema_db_uri'
app.config['SQLALCHEMY_BINDS'] = {'other_schema': 'your_other_db_uri'}

class TableA(db.Model):
    # This belongs to Default schema, it doesn't need specify __bind_key__
    ...

class TableB(db.Model):
      # This belongs to other_schema
    __bind_key__ = 'other_schema'
    ...


来源:https://stackoverflow.com/questions/11873959/flask-sqlalchemy-postgresql-define-specific-schema-for-table

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