将sqlalchemy-continuum与flask-sqlalchemy和flask-migrate一起使用

时间:2018-06-27 14:35:12

标签: python flask flask-sqlalchemy sqlalchemy-continuum

我正在尝试使sqlalchemy-continuum与flask-sqlalchemy和flask-migrate一起使用。我的__init__.py文件如下所示:

import os

from flask import Flask

def create_app():
    """Create and configure an instance of the Flask application."""
    app = Flask(__name__, instance_relative_config=True)

    app.config.from_mapping(
        SQLALCHEMY_DATABASE_URI='postgres+psycopg2://{}:{}@{}:{}/{}'.format(
            os.environ['POSTGRES_USER'],
            os.environ['POSTGRES_PASSWORD'],
            os.environ['POSTGRES_HOST'],
            os.environ['POSTGRES_PORT'],
            os.environ['POSTGRES_DB']
        ),
        SQLALCHEMY_TRACK_MODIFICATIONS=False
    )

    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    from .models import db, migrate
    db.init_app(app)
    migrate.init_app(app, db)

    return app

我的models.py文件如下:

import sqlalchemy
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from sqlalchemy_continuum import make_versioned


db = SQLAlchemy()
migrate = Migrate()

make_versioned(user_cls=None)

class User(db.Model):
    __versioned__ = {}
    __tablename__ = 'user'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    password = db.Column(db.String(20), unique=True, nullable=False)

    def __repr__(self):
        return '<User {} - {}>'.format(self.username, self.email)

sqlalchemy.orm.configure_mappers()

然后我运行以下flask-migrate命令来初始化和迁移数据库:

flask db init
flask db migrate
flask db upgrade

flask db upgrade命令的输出似乎显示正在创建的正确表:

INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added table 'transaction'
INFO  [alembic.autogenerate.compare] Detected added table 'user'
INFO  [alembic.autogenerate.compare] Detected added table 'user_version'
INFO  [alembic.autogenerate.compare] Detected added index 'ix_user_version_end_transaction_id' on '['end_transaction_id']'
INFO  [alembic.autogenerate.compare] Detected added index 'ix_user_version_operation_type' on '['operation_type']'
INFO  [alembic.autogenerate.compare] Detected added index 'ix_user_version_transaction_id' on '['transaction_id']'

在python shell中,我可以执行以下操作:

>>> from test_flask.__init__ import create_app
>>> from test_flask.models import db, User

>>> app = create_app()
>>> with app.app_context():
...     user = User(username='devuser', email='devuser@gmail.com', 
password='devpassword')
...     db.session.add(user)
...     db.session.commit()

这似乎工作正常,但是当我尝试使用以下方式访问版本属性中的元素时:

>>> user.versions[0]

我收到以下错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/dynamic.py", line 254, in __getitem__
  attributes.PASSIVE_NO_INITIALIZE).indexed(index)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/dynamic.py", line 359, in indexed
  return list(self.added_items)[index]
IndexError: list index out of range

命令:

>>> user.versions

返回:

<sqlalchemy.orm.dynamic.AppenderQuery object at 0x7f6515d3a898>

这似乎不是sqlalchemy-continuum docs中指定的版本属性的预期行为。关于我做错了什么的想法?

1 个答案:

答案 0 :(得分:2)

至少有两种情况可能会发生此错误:

  • 您没有在UPDATEINSERT之后提交
  • 或者您不在app上下文中

在烧瓶瓶的示例中,所有语句应位于上下文块内:

>>> with app.app_context():
...     user = User(username='devuser2', email='devuser2@gmail.com', password='devpassword2')
...     db.session.add(user)
...     db.session.commit()
...     user.versions[0].username
...     user.username='devuser_fixed'
...     db.session.commit()    
...     user.versions[1].username

#'devuser'
#'devuser_fixed'

在上下文之外,user仍然存在,变量保留在内存中,但是连接到数据库的会话丢失了。


注意:如果缺少第二个提交,则user.versions[1]仅在实际上下文会话中可用。因此,如果退出此上下文并选中user.versions[1],则会遇到相同的错误。