Skip to content

Commit

Permalink
Fixed using .first()
Browse files Browse the repository at this point in the history
fixed model attribute containing a list instead of a model when using .first()
added additional tests checking .first() usage
  • Loading branch information
circulon committed Oct 21, 2024
1 parent ad33eff commit d377d37
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
17 changes: 6 additions & 11 deletions src/masoniteorm/relationships/HasOneThrough.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,8 @@ def register_related(self, key, model, collection):
None
"""

related_id = getattr(model, self.local_key)
for id, item in collection.items():
if id == related_id:
model.add_relation({key: item[0]})
break
related = collection.get(getattr(model, self.local_key), None)
model.add_relation({key: related[0] if related else None})

def get_related(self, current_builder, relation, eagers=None, callback=None):
"""
Expand Down Expand Up @@ -175,17 +172,15 @@ def get_related(self, current_builder, relation, eagers=None, callback=None):
))

if isinstance(relation, Collection):
self.distant_builder.where_in(
return self.distant_builder.where_in(
f"{int_table}.{self.local_owner_key}",
Collection(relation._get_value(self.local_key)).unique(),
)
).get()
else:
self.distant_builder.where(
return self.distant_builder.where(
f"{int_table}.{self.local_owner_key}",
getattr(relation, self.local_key),
)

return self.distant_builder.get()
).first()

def attach(self, current_model, related_record):
raise NotImplementedError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,39 @@ def test_has_one_through_can_eager_load(self):
self.assertIsInstance(shipment2.from_country, Country)
self.assertEqual(shipment2.from_country.country_id, 40)

# check .first() and .get() produce the same result
single = (
IncomingShipment.where("name", "Tractor Parts")
.with_("from_country")
.first()
)
single_get = (
IncomingShipment.where("name", "Tractor Parts").with_("from_country").get()
)
self.assertEqual(single.from_country.country_id, 10)
self.assertEqual(single_get.count(), 1)
self.assertEqual(
single.from_country.country_id, single_get.first().from_country.country_id
)

def test_has_one_through_eager_load_can_be_empty(self):
shipments = (
IncomingShipment.where("name", "Bread")
.where_has("from_country", lambda query: query.where("name", "Ueaguay"))
.with_(
"from_country",
)
.get()
)
self.assertEqual(shipments.count(), 2)
self.assertEqual(shipments.count(), 0)

def test_has_one_through_can_get_related(self):
shipment = IncomingShipment.where("name", "Milk").first()
self.assertIsInstance(shipment.from_country, Country)
self.assertEqual(shipment.from_country.country_id, 10)

def test_has_one_through_has_query(self):
shipments = IncomingShipment.where_has(
"from_country", lambda query: query.where("name", "USA")
)
self.assertEqual(shipments.count(), 2)

0 comments on commit d377d37

Please sign in to comment.