我正在尝试让 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 文档中指定的版本属性的预期行为。关于我做错了什么有什么想法吗?