Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability so specify default select criteria for Models #904

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/masoniteorm/models/Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,10 @@ def get_builder(self):
dry=self.__dry__,
)

return self.builder.select(*self.__selects__)
return self.builder.select(*self.get_selects())

def get_selects(self):
return self.__selects__

@classmethod
def get_columns(cls):
Expand Down
20 changes: 20 additions & 0 deletions tests/models/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class ModelTestForced(Model):
__table__ = "users"
__force_update__ = True

class BaseModel(Model):
def get_selects(self):
return [f"{self.get_table_name()}.*"]

class ModelWithBaseModel(BaseModel):
__table__ = "users"

class TestModels(unittest.TestCase):
def test_model_can_access_str_dates_as_pendulum(self):
Expand Down Expand Up @@ -253,3 +259,17 @@ def test_both_fillable_and_guarded_attributes_raise(self):
# Removing one of the props allows us to instantiate
delattr(InvalidFillableGuardedModelTest, "__guarded__")
InvalidFillableGuardedModelTest()

def test_model_can_provide_default_select(self):
sql = ModelWithBaseModel.to_sql()
self.assertEqual(
sql,
"""SELECT `users`.* FROM `users`""",
)

def test_model_can_add_to_default_select(self):
sql = ModelWithBaseModel.select(["products.name", "products.id", "store.name"]).to_sql()
self.assertEqual(
sql,
"""SELECT `users`.*, `products`.`name`, `products`.`id`, `store`.`name` FROM `users`""",
)
Loading