使Flask-Migrate忽略映射为Flask-SQLAlchemy模型的SQL视图 [英] Getting Flask-Migrate to Ignore SQL Views that are mapped as Flask-SQLAlchemy Models

查看:220
本文介绍了使Flask-Migrate忽略映射为Flask-SQLAlchemy模型的SQL视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flask-SQLAlchemy定义我的模型,然后使用Flask-Migrate自动生成迁移脚本以部署到PostgreSQL数据库中.我在我的应用程序中使用的数据库上定义了许多SQL视图,如下所示.

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.

但是,由于Flask-Migrate认为它是表,因此它现在为视图生成迁移文件.如何正确使Flask-Migrate/Alembic在自动生成过程中忽略视图?

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视图名称:vw_SampleView,带有两列:idrowcount.

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()

我尝试按照 http://alembic.zzzcomputing.com/en/上的说明进行操作Latest/cookbook.html ,并将info = dict(is_view=True)部分添加到我的模型中,并将以下位添加到我的env.py文件中,但是不知道从这里开始.

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)

推荐答案

我认为(尽管尚未测试)您可以使用__table_args__属性将表标记为视图:

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)

这篇关于使Flask-Migrate忽略映射为Flask-SQLAlchemy模型的SQL视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆