Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Nov 23, 2024
1 parent fe4950f commit 40b2184
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/masoniteorm/models/Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1216,11 +1216,11 @@ def __call__(self):
this is returned when you do model.relationship()
"""
print("Calling relationship")
return self.relationship
return self.relationship.apply_query()

# def __repr__(self):
# return repr(self.relationship)

def __iter__(self):
print("iterating relationship propery")
return iter(self.relationship) # Use the iterator of the list
return iter(self.relationship)
46 changes: 46 additions & 0 deletions tests/playground/test_playground.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import unittest
from src.masoniteorm.models import Model

class CreditCard(Model):
__table__ = "company_credit_cards"
__primary_key__ = "credit_card_id"


class Company(Model):
__table__ = "tbl_companies"
__primary_key__ = "company_id"

@property
def cards(self):
"""
Return a RelationshipProperty wrapper for dual behavior.
"""
return self.has_many(CreditCard, "company_id", "company_id")

class User(Model):
__table__ = "tbl_users"
__primary_key__ = "user_id"

@property
def company(self):
"""
Return a RelationshipProperty wrapper for dual behavior.
"""
return self.has_one(Company, "company_id", "company_id")


class PlaygroundTest(unittest.TestCase):
def test_has_one_can_access_related_model_attribute(self):
user = User.find(667)

# Access the related company instance as a property
related_company = user.company
self.assertEqual(related_company.company_name, "ROTHCO ACCOUNT")

def test_has_one_can_build_off_query_builder(self):
user = User.find(667)

# Access the related company instance as a property
related_company = user.company
# self.assertEqual(related_company.company_name, "ROTHCO ACCOUNT")
self.assertEqual(user.company().to_sql(), "SELECT * FROM `tbl_companies` WHERE `tbl_companies`.`company_id` = '373849'")

0 comments on commit 40b2184

Please sign in to comment.