title: Using sqlalchemy-continuum with flask-sqlalchemy and flask-migrate
body: <p>I'm trying to get sqlalchemy-continuum to work alongside flask-sqlalchemy and flask-migrate. My <code>__init__.py</code> file looks like this:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>My models.py file looks like this:</p>
  
  <pre><code>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 '&lt;User {} - {}&gt;'.format(self.username, self.email)
  
  sqlalchemy.orm.configure_mappers()
  </code></pre>
  
  <p>I then run the following flask-migrate commands to initialise and migrate the database:</p>
  
  <pre><code>flask db init
  flask db migrate
  flask db upgrade
  </code></pre>
  
  <p>The output of the flask db upgrade command seems to show the correct tables being created:</p>
  
  <pre><code>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']'
  </code></pre>
  
  <p>In the python shell I can do the following:</p>
  
  <pre><code>&gt;&gt;&gt; from test_flask.__init__ import create_app
  &gt;&gt;&gt; from test_flask.models import db, User
  
  &gt;&gt;&gt; app = create_app()
  &gt;&gt;&gt; with app.app_context():
  ...     user = User(username='devuser', email='devuser@gmail.com', 
  password='devpassword')
  ...     db.session.add(user)
  ...     db.session.commit()
  </code></pre>
  
  <p>This seems to work fine but when I attempt to access an element in the versions attribute using:</p>
  
  <pre><code>&gt;&gt;&gt; user.versions[0]
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  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
  </code></pre>
  
  <p>The command:</p>
  
  <pre><code>&gt;&gt;&gt; user.versions
  </code></pre>
  
  <p>returns:</p>
  
  <pre><code>&lt;sqlalchemy.orm.dynamic.AppenderQuery object at 0x7f6515d3a898&gt;
  </code></pre>
  
  <p>This doesn't seem to be the expected behaviour of the versions attribute, as specified in the <a href="https://sqlalchemy-continuum.readthedocs.io/en/latest/intro.html#versions-and-transactions" rel="nofollow noreferrer">sqlalchemy-continuum docs</a>. Any ideas as to what I've done wrong?</p>