Skip to content

AttributeError: 'property' object has no attribute 'impl' when using model_class in flask-sqlalchemy #216

Open

Description

@rosensama

Not sure if this is an issue for sqlalchemy-continuum or flask-sqlalchemy, but my gut says to try here first. As best I can tell it occurs when processing TestVersion.version_parent

I'd expect that the Test model would inherit both __versioned__ and the id column from the Common mix-in, but instead I get the following error. The same error occurs in other usage of the Test model.

I am using current releases of all packages.

  • Flask 1.0.2
  • SQLAlchemy 1.2.18
  • Flask-SQLAlchemy 2.3.2
  • SQLAlchemy-Continuum 1.3.7

Thanks in advance for taking a look.

$ flask db migrate
Traceback (most recent call last):
  File "/Users/rosensama/.local/share/virtualenvs/projcomp-p5c4OIoo/bin/flask", line 10, in <module>
    sys.exit(main())
  File "/Users/rosensama/.local/share/virtualenvs/projcomp-p5c4OIoo/lib/python3.7/site-packages/flask/cli.py", line 894, in main
    cli.main(args=args, prog_name=name)
  File "/Users/rosensama/.local/share/virtualenvs/projcomp-p5c4OIoo/lib/python3.7/site-packages/flask/cli.py", line 557, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "/Users/rosensama/.local/share/virtualenvs/projcomp-p5c4OIoo/lib/python3.7/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/Users/rosensama/.local/share/virtualenvs/projcomp-p5c4OIoo/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/rosensama/.local/share/virtualenvs/projcomp-p5c4OIoo/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/rosensama/.local/share/virtualenvs/projcomp-p5c4OIoo/lib/python3.7/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/rosensama/.local/share/virtualenvs/projcomp-p5c4OIoo/lib/python3.7/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/Users/rosensama/.local/share/virtualenvs/projcomp-p5c4OIoo/lib/python3.7/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/Users/rosensama/.local/share/virtualenvs/projcomp-p5c4OIoo/lib/python3.7/site-packages/flask/cli.py", line 411, in decorator
    with __ctx.ensure_object(ScriptInfo).load_app().app_context():
  File "/Users/rosensama/.local/share/virtualenvs/projcomp-p5c4OIoo/lib/python3.7/site-packages/flask/cli.py", line 372, in load_app
    app = locate_app(self, import_name, name)
  File "/Users/rosensama/.local/share/virtualenvs/projcomp-p5c4OIoo/lib/python3.7/site-packages/flask/cli.py", line 235, in locate_app
    __import__(module_name)
  File "/Users/rosensama/projects/projcomp/server/server.py", line 7, in <module>
    from server.models import Test
  File "/Users/rosensama/projects/projcomp/server/models.py", line 113, in <module>
    configure_mappers()
  File "/Users/rosensama/.local/share/virtualenvs/projcomp-p5c4OIoo/lib/python3.7/site-packages/sqlalchemy/orm/mapper.py", line 3210, in configure_mappers
    Mapper.dispatch._for_class(Mapper).after_configured()
  File "/Users/rosensama/.local/share/virtualenvs/projcomp-p5c4OIoo/lib/python3.7/site-packages/sqlalchemy/event/attr.py", line 259, in __call__
    fn(*args, **kw)
  File "/Users/rosensama/.local/share/virtualenvs/projcomp-p5c4OIoo/lib/python3.7/site-packages/sqlalchemy/orm/events.py", line 637, in wrap
    fn(*arg, **kw)
  File "/Users/rosensama/.local/share/virtualenvs/projcomp-p5c4OIoo/lib/python3.7/site-packages/sqlalchemy_continuum/builder.py", line 178, in configure_versioned_classes
    getattr(cls, prop.key).impl.active_history = True
AttributeError: 'property' object has no attribute 'impl'

server/__init__.py

from flask import Flask


def create_app():
    app = Flask(__name__)
    app.config["SQLALCHEMY_DATABASE_URI"] = DB_URI
    app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

    from server.models import db
    from server.models import migrate

    db.init_app(app)
    migrate.init_app(app, db)

    return app

server/server.py

from server import create_app
from server.models import Test


app = create_app()


@app.route("/")
def hello_world():
    return "Hello World!"


if __name__ == "__main__":
    app.run()

server/models.py

from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from sqlalchemy_continuum import make_versioned
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy.orm import configure_mappers


class Common:
    __versioned__ = {}
    id = Column(Integer, primary_key=True)


db = SQLAlchemy(model_class=Common)
migrate = Migrate()
make_versioned(user_cls=None)


class Test(db.Model):
    name = Column(String)


configure_mappers()

Activity

  1. malikolivier commented on Mar 11, 2019

    @malikolivier

    I'd expect that the Test model would inherit [...] __versioned__

    I'd be very cautious with this assumption, as I don't think such behavior is documented. Did you try setting __versioned__ on Test?

    I got sqlalchemy-continuum working without issue on a flask app by adding __versioned__ to each model I wanted to version, as the doc says. My flask app did not set model_class in SQLAlchemy however.

  2. rosensama commented on Mar 12, 2019

    @rosensama
    Author

    Thanks for taking a look

    I don't know if it's supported by sqlalchemy-continuum, but it's supported by SQLAlchemy and flask-sqlalchemy.

    https://docs.sqlalchemy.org/en/latest/orm/extensions/declarative/mixins.html#augmenting-the-base
    http://flask-sqlalchemy.pocoo.org/2.3/customizing/

    I've also tried having Common inherit from flask_sqlalchemy.Model, but no change in results.

    Passing model_class to sqlalchemy doesn't seem to work, but I can declare my models with the same mixin without issue.

    class Test(db.Model, Common):
        name = Column(String)

    I guess this could be an issue with flask-migrate or sqlalchmey-continuum, but my example does work if I leave id in place, commenting out __versioned__ I 've also tried the following.

    class Common:
        @declared_attr
        def __versioned__(cls):
            return {}
  3. malikolivier commented on Mar 12, 2019

    @malikolivier

    From my experience, there are many different ways to use sqlalchemy and not all of them are compatible with all the libraries in the sqlalchemy ecosystem (barring version issues).

    I don't know whether the author of this project would consider the behavior that you reported as a bug.

    In a personal project, I integrate sqlalchemy, flask-sqlalchemy and sqlalchemy-continuum without much issue, and I do not use the model_class approach. I define my models as below:

    class Test(db.Model, Common):
        __versioned__ = {}
        name = db.Column(db.String)
  4. added
    pinned
    on Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pinned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions