Getting Flask-Migrate to Ignore SQL Views that are mapped as Flask-SQLAlchemy Models

谁说我不能喝 提交于 2019-12-01 21:46:46

问题


I am using Flask-SQLAlchemy to define my models, and then using Flask-Migrate to auto-generate migration scripts for deployment onto a PostgreSQL database. I have defined a number of SQL Views on the database that I use in my application like below.

However, Flask-Migrate now generates a migration file for the view as it thinks it's a table. How do I correctly get Flask-Migrate / Alembic to ignore the view during autogenerate?

SQL View name: vw_SampleView with two columns: id and rowcount.

class ViewSampleView(db.Model):
    __tablename__ = 'vw_report_high_level_count'

    info = dict(is_view=True)

    id = db.Column(db.String(), primary_key=True)
    rowcount = db.Column(db.Integer(), nullable=False)

Which means I can now do queries like so:

ViewSampleView.query.all()

I tried following instructions on http://alembic.zzzcomputing.com/en/latest/cookbook.html and added the info = dict(is_view=True) portion to my model and the following bits to my env.py file, but don't know where to go from here.

def include_object(object, name, type_, reflected, compare_to):
    """
    Exclude views from Alembic's consideration.
    """

    return not object.info.get('is_view', False)

...

context.configure(url=url,include_object = include_object)

回答1:


I think (though haven't tested) that you can mark your Table as a view with the __table_args__ attribute:

class ViewSampleView(db.Model):
    __tablename__ = 'vw_report_high_level_count'
    __table_args__ = {'info': dict(is_view=True)}

id = db.Column(db.String(), primary_key=True)
rowcount = db.Column(db.Integer(), nullable=False)


来源:https://stackoverflow.com/questions/41989969/getting-flask-migrate-to-ignore-sql-views-that-are-mapped-as-flask-sqlalchemy-mo

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