Skip to content

Commit 500e732

Browse files
authored
Merge pull request pallets-eco#651 from minisotm/master
fix BindMetaMixin with polymorphic
2 parents 50944e7 + 68c72b8 commit 500e732

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

CHANGES.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
Version 2.3.3
5+
-------------
6+
7+
Fix "AttributeError: 'NoneType' object has no attribute 'info'", when using polymorphic models. (`#651`_)
8+
9+
.. _#651: https://github.com/mitsuhiko/flask-sqlalchemy/pull/651
10+
411

512
Version 2.3.2
613
-------------

flask_sqlalchemy/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def __init__(cls, name, bases, d):
120120

121121
super(BindMetaMixin, cls).__init__(name, bases, d)
122122

123-
if bind_key is not None and hasattr(cls, '__table__'):
123+
if bind_key is not None and getattr(cls, '__table__', None) is not None:
124124
cls.__table__.info['bind_key'] = bind_key
125125

126126

tests/test_binds.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import flask_sqlalchemy as fsa
2-
2+
from sqlalchemy.ext.declarative import declared_attr
33

44
def test_basic_binds(app, db):
55
app.config['SQLALCHEMY_BINDS'] = {
@@ -91,3 +91,35 @@ def test_connector_cache(app):
9191

9292
connector = fsa.get_state(app).connectors[None]
9393
assert connector._app is app
94+
95+
96+
def test_polymorphic_bind(app, db):
97+
bind_key = 'polymorphic_bind_key'
98+
99+
app.config['SQLALCHEMY_BINDS'] = {
100+
bind_key: 'sqlite:///:memory',
101+
}
102+
103+
class Base(db.Model):
104+
__bind_key__ = bind_key
105+
106+
__tablename__ = 'base'
107+
108+
id = db.Column(db.Integer, primary_key=True)
109+
110+
p_type = db.Column(db.String(50))
111+
112+
__mapper_args__ = {
113+
'polymorphic_identity': 'base',
114+
'polymorphic_on': p_type
115+
}
116+
117+
class Child1(Base):
118+
119+
child_1_data = db.Column(db.String(50))
120+
__mapper_args__ = {
121+
'polymorphic_identity': 'child_1',
122+
}
123+
124+
assert Base.__table__.info['bind_key'] == bind_key
125+
assert Child1.__table__.info['bind_key'] == bind_key

0 commit comments

Comments
 (0)