我想使用flask-sqlalchemy为'model‘定义一个特定的模式。当您在sqlalchemy本身中创建表对象时,它有一个要作为模式名传递的参数。
我如何在flask-sqlalchemy中做到这一点?
发布于 2012-08-09 10:03:13
在定义模型类时,请使用:
__table_args__ = {"schema":"schema_name"}也许这会让其他人省去一些打猎的时间。
发布于 2015-06-26 06:59:41
对于将来的参考:
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'
...发布于 2020-08-03 08:44:54
这就是我怎么做的,但如果可能的话,我也在寻找一种更好的方法-
db = flask.ext.sqlalchemy.SQLAlchemy(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'your_default_schema_db_uri'
class Entity():
__table_args__ = {
'schema': 'schema_name'
}
# common columns below
class TableA(Entity, db.Model):
# This belongs to Default schema, it doesn't need specify __bind_key__
__table_args__ = {'schema' : Entity.__table_args__["schema"],
'comment': 'TableA'}
class TableB(Entity, db.Model):
# This belongs to other_schema
__table_args__ = {'schema' : Entity.__table_args__["schema"],
'comment': 'TableA'}https://stackoverflow.com/questions/11873959
复制相似问题