Skip to content

Commit ca9bc1c

Browse files
committed
add nes tests
1 parent 6f644e3 commit ca9bc1c

File tree

3 files changed

+755
-36
lines changed

3 files changed

+755
-36
lines changed

src/strawberry_sqlalchemy_mapper/loader.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ async def load_fn(keys: List[Tuple]) -> List[Any]:
8181
related_model_key_label = relationship.local_remote_pairs[1][1].key
8282

8383
self_model_key = relationship.local_remote_pairs[0][0].key
84+
related_model_key = relationship.local_remote_pairs[1][0].key
8485

8586
remote_to_use = relationship.local_remote_pairs[0][1]
8687
query_keys = tuple([item[0] for item in keys])
8788

89+
# This query returns every row equal (self_model.key, related_model)
8890
query = (
8991
select(
9092
label(self_model_key_label, getattr(
@@ -94,12 +96,12 @@ async def load_fn(keys: List[Tuple]) -> List[Any]:
9496
.join(
9597
relationship.secondary,
9698
getattr(relationship.secondary.c,
97-
related_model_key_label) == related_model.id
99+
related_model_key_label) == getattr(related_model, related_model_key)
98100
)
99101
.join(
100102
self_model,
101103
getattr(relationship.secondary.c,
102-
self_model_key_label) == self_model.id
104+
self_model_key_label) == getattr(self_model, self_model_key)
103105
)
104106
.filter(
105107
remote_to_use.in_(query_keys)

src/strawberry_sqlalchemy_mapper/mapper.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -517,24 +517,16 @@ async def resolve(self, info: Info):
517517
]
518518
)
519519
else:
520-
# If has a secondary table, gets only the first id since the other id cannot be get without a query
521-
# breakpoint()
522-
local_remote_pairs_secondary_table_local = relationship.local_remote_pairs[0][0]
520+
# If has a secondary table, gets only the first ID as additional IDs require a separate query
521+
local_remote_pairs_secondary_table_local = relationship.local_remote_pairs[
522+
0][0]
523523
relationship_key = tuple(
524524
[
525-
getattr(self, local_remote_pairs_secondary_table_local.key),
525+
getattr(
526+
self, local_remote_pairs_secondary_table_local.key),
526527
]
527528
)
528529

529-
# relationship_key = tuple(
530-
# [
531-
# getattr(self, local.key)
532-
# for local, _ in relationship.local_remote_pairs or []
533-
# if local.key
534-
# ]
535-
# )
536-
# breakpoint()
537-
538530
if any(item is None for item in relationship_key):
539531
if relationship.uselist:
540532
return []
@@ -544,7 +536,6 @@ async def resolve(self, info: Info):
544536
loader = info.context["sqlalchemy_loader"]
545537
else:
546538
loader = info.context.sqlalchemy_loader
547-
# breakpoint()
548539
related_objects = await loader.loader_for(relationship).load(
549540
relationship_key
550541
)

0 commit comments

Comments
 (0)