Skip to content

Commit

Permalink
Merge pull request pallets-eco#651 from minisotm/master
Browse files Browse the repository at this point in the history
fix BindMetaMixin with polymorphic
  • Loading branch information
davidism authored Oct 30, 2018
2 parents 50944e7 + 68c72b8 commit 500e732
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -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
-------------
Expand Down
2 changes: 1 addition & 1 deletion flask_sqlalchemy/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
34 changes: 33 additions & 1 deletion tests/test_binds.py
Original file line number Diff line number Diff line change
@@ -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'] = {
Expand Down Expand Up @@ -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

0 comments on commit 500e732

Please sign in to comment.