From 5a3422e01fa63062dad7bc0f98943c5934a05bb6 Mon Sep 17 00:00:00 2001 From: Aleksei Maslennikov Date: Mon, 29 Oct 2018 17:34:42 +0300 Subject: [PATCH 1/2] fix BindMetaMixin with polymorphic --- flask_sqlalchemy/model.py | 2 +- tests/test_binds.py | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/flask_sqlalchemy/model.py b/flask_sqlalchemy/model.py index d34e1f10..63419820 100644 --- a/flask_sqlalchemy/model.py +++ b/flask_sqlalchemy/model.py @@ -120,7 +120,7 @@ def __init__(cls, name, bases, d): super(BindMetaMixin, cls).__init__(name, bases, d) - if bind_key is not None and hasattr(cls, '__table__'): + if bind_key is not None and getattr(cls, '__table__', None) is not None: cls.__table__.info['bind_key'] = bind_key diff --git a/tests/test_binds.py b/tests/test_binds.py index 19ce19f4..9fd8a16b 100644 --- a/tests/test_binds.py +++ b/tests/test_binds.py @@ -1,5 +1,5 @@ import flask_sqlalchemy as fsa - +from sqlalchemy.ext.declarative import declared_attr def test_basic_binds(app, db): app.config['SQLALCHEMY_BINDS'] = { @@ -91,3 +91,35 @@ def test_connector_cache(app): connector = fsa.get_state(app).connectors[None] assert connector._app is app + + +def test_polymorphic_bind(app, db): + bind_key = 'polymorphic_bind_key' + + app.config['SQLALCHEMY_BINDS'] = { + bind_key: 'sqlite:///:memory', + } + + class Base(db.Model): + __bind_key__ = bind_key + + __tablename__ = 'base' + + id = db.Column(db.Integer, primary_key=True) + + p_type = db.Column(db.String(50)) + + __mapper_args__ = { + 'polymorphic_identity': 'base', + 'polymorphic_on': p_type + } + + class Child1(Base): + + child_1_data = db.Column(db.String(50)) + __mapper_args__ = { + 'polymorphic_identity': 'child_1', + } + + assert Base.__table__.info['bind_key'] == bind_key + assert Child1.__table__.info['bind_key'] == bind_key From 68c72b833c33e2f0ad335ba519dffaaf56213b32 Mon Sep 17 00:00:00 2001 From: Aleksei Maslennikov Date: Mon, 29 Oct 2018 21:05:00 +0300 Subject: [PATCH 2/2] add changelog by 651 --- CHANGES.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 4573e2a7..a3ea91c0 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,13 @@ Changelog ========= +Version 2.3.3 +------------- + +Fix "AttributeError: 'NoneType' object has no attribute 'info'", when using polymorphic models. (`#651`_) + +.. _#651: https://github.com/mitsuhiko/flask-sqlalchemy/pull/651 + Version 2.3.2 -------------