Skip to content

Commit b26fecc

Browse files
authored
Merge pull request #782 from pallets/backport-651
backport #651 fix BindMetaMixin with polymorphic
2 parents 40a53b2 + 37779fb commit b26fecc

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

CHANGES.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
Version 2.4.1
2+
-------------
3+
4+
Unreleased
5+
6+
- Fix ``AttributeError`` when using multiple binds with polymorphic
7+
models. :pr:`651`
8+
9+
110
Version 2.4.0
211
-------------
312

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)