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

Fixed mass assign issue on save #842

Merged
merged 2 commits into from
Feb 19, 2023
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
9 changes: 7 additions & 2 deletions src/masoniteorm/models/Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,20 +863,25 @@ def save(self, query=False):

if not query:
if self.is_loaded():
result = builder.update(self.__dirty_attributes__)
result = builder.update(
self.__dirty_attributes__, ignore_mass_assignment=True
)
else:
result = self.create(
self.__dirty_attributes__,
query=query,
id_key=self.get_primary_key(),
ignore_mass_assignment=True,
)
self.observe_events(self, "saved")
self.fill(result.__attributes__)
self.__dirty_attributes__ = {}
return result

if self.is_loaded():
result = builder.update(self.__dirty_attributes__, dry=query).to_sql()
result = builder.update(
self.__dirty_attributes__, dry=query, ignore_mass_assignment=True
).to_sql()
else:
result = self.create(self.__dirty_attributes__, query=query)

Expand Down
9 changes: 7 additions & 2 deletions src/masoniteorm/query/QueryBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ def create(
query: bool = False,
id_key: str = "id",
cast: bool = False,
ignore_mass_assignment: bool = False,
**kwargs,
):
"""Specifies a dictionary that should be used to create new values.
Expand All @@ -518,7 +519,8 @@ def create(
# Update values with related record's
self._creates.update(self._creates_related)
# Filter __fillable/__guarded__ fields
self._creates = model.filter_mass_assignment(self._creates)
if not ignore_mass_assignment:
self._creates = model.filter_mass_assignment(self._creates)
# Cast values if necessary
if cast:
self._creates = model.cast_values(self._creates)
Expand Down Expand Up @@ -1388,6 +1390,7 @@ def update(
dry: bool = False,
force: bool = False,
cast: bool = False,
ignore_mass_assignment: bool = False,
):
"""Specifies columns and values to be updated.

Expand All @@ -1396,6 +1399,7 @@ def update(
dry {bool, optional}: Do everything except execute the query against the DB
force {bool, optional}: Force an update statement to be executed even if nothing was changed
cast {bool, optional}: Run all values through model's casters
ignore_mass_assignment {bool, optional}: Whether the update should ignore mass assignment on the model

Returns:
self
Expand All @@ -1407,7 +1411,8 @@ def update(
if self._model:
model = self._model
# Filter __fillable/__guarded__ fields
updates = model.filter_mass_assignment(updates)
if not ignore_mass_assignment:
updates = model.filter_mass_assignment(updates)

if model and model.is_loaded():
self.where(model.get_primary_key(), model.get_primary_key_value())
Expand Down