Skip to content

Commit

Permalink
Merge pull request #904 from circulon/feature/902
Browse files Browse the repository at this point in the history
Added ability so specify default select criteria for Models
  • Loading branch information
josephmancuso authored Oct 30, 2024
2 parents 15894aa + f0c2359 commit 3451fd6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
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`""",
)

0 comments on commit 3451fd6

Please sign in to comment.