Skip to content

Commit b49c63e

Browse files
committed
fix: throw error when association proxy could not be converted
Signed-off-by: Erik Wrede <[email protected]>
1 parent 34b5742 commit b49c63e

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

graphene_sqlalchemy/converter.py

+7
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ def dynamic_type():
9191
assoc_prop.value_attr,
9292
**field_kwargs,
9393
).get_type()
94+
else:
95+
raise TypeError(
96+
"Unsupported association proxy target type: {} for prop {} on type {}. "
97+
"Please disable the conversion of this field using an ORMField.".format(
98+
type(attr), assoc_prop, obj_type
99+
)
100+
)
94101
# else, not supported
95102

96103
return graphene.Dynamic(dynamic_type)

graphene_sqlalchemy/tests/models.py

+12
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ def __repr__(self):
6868
return "{} {}".format(self.first_name, self.last_name)
6969

7070

71+
class ProxiedReporter(Base):
72+
__tablename__ = "reporters_error"
73+
id = Column(Integer(), primary_key=True)
74+
first_name = Column(String(30), doc="First name")
75+
last_name = Column(String(30), doc="Last name")
76+
reporter_id = Column(Integer(), ForeignKey("reporters.id"))
77+
reporter = relationship("Reporter", uselist=False)
78+
79+
# This is a hybrid property, we don't support proxies on hybrids yet
80+
composite_prop = association_proxy("reporter", "composite_prop")
81+
82+
7183
class Reporter(Base):
7284
__tablename__ = "reporters"
7385

graphene_sqlalchemy/tests/test_converter.py

+20
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
Article,
3030
CompositeFullName,
3131
Pet,
32+
ProxiedReporter,
3233
Reporter,
3334
ShoppingCart,
3435
ShoppingCartItem,
@@ -523,6 +524,25 @@ class Meta:
523524
assert dynamic_field.get_type().type.of_type == ArticleType
524525

525526

527+
def test_should_throw_error_association_proxy_unsupported_target():
528+
class ProxiedReporterType(SQLAlchemyObjectType):
529+
class Meta:
530+
model = ProxiedReporter
531+
532+
field = convert_sqlalchemy_association_proxy(
533+
ProxiedReporter,
534+
ProxiedReporter.composite_prop,
535+
ProxiedReporterType,
536+
get_global_registry(),
537+
default_connection_field_factory,
538+
True,
539+
mock_resolver,
540+
)
541+
542+
with pytest.raises(TypeError):
543+
field.get_type()
544+
545+
526546
def test_should_postgresql_uuid_convert():
527547
assert get_field(postgresql.UUID()).type == graphene.UUID
528548

0 commit comments

Comments
 (0)