Skip to content

Commit 882e677

Browse files
Merge pull request #842 from MasoniteFramework/fix/mass-assign-issue-on-save
Fixed mass assign issue on save
2 parents 4560c1c + c4fd787 commit 882e677

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/masoniteorm/models/Model.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -863,20 +863,25 @@ def save(self, query=False):
863863

864864
if not query:
865865
if self.is_loaded():
866-
result = builder.update(self.__dirty_attributes__)
866+
result = builder.update(
867+
self.__dirty_attributes__, ignore_mass_assignment=True
868+
)
867869
else:
868870
result = self.create(
869871
self.__dirty_attributes__,
870872
query=query,
871873
id_key=self.get_primary_key(),
874+
ignore_mass_assignment=True,
872875
)
873876
self.observe_events(self, "saved")
874877
self.fill(result.__attributes__)
875878
self.__dirty_attributes__ = {}
876879
return result
877880

878881
if self.is_loaded():
879-
result = builder.update(self.__dirty_attributes__, dry=query).to_sql()
882+
result = builder.update(
883+
self.__dirty_attributes__, dry=query, ignore_mass_assignment=True
884+
).to_sql()
880885
else:
881886
result = self.create(self.__dirty_attributes__, query=query)
882887

src/masoniteorm/query/QueryBuilder.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ def create(
499499
query: bool = False,
500500
id_key: str = "id",
501501
cast: bool = False,
502+
ignore_mass_assignment: bool = False,
502503
**kwargs,
503504
):
504505
"""Specifies a dictionary that should be used to create new values.
@@ -518,7 +519,8 @@ def create(
518519
# Update values with related record's
519520
self._creates.update(self._creates_related)
520521
# Filter __fillable/__guarded__ fields
521-
self._creates = model.filter_mass_assignment(self._creates)
522+
if not ignore_mass_assignment:
523+
self._creates = model.filter_mass_assignment(self._creates)
522524
# Cast values if necessary
523525
if cast:
524526
self._creates = model.cast_values(self._creates)
@@ -1388,6 +1390,7 @@ def update(
13881390
dry: bool = False,
13891391
force: bool = False,
13901392
cast: bool = False,
1393+
ignore_mass_assignment: bool = False,
13911394
):
13921395
"""Specifies columns and values to be updated.
13931396
@@ -1396,6 +1399,7 @@ def update(
13961399
dry {bool, optional}: Do everything except execute the query against the DB
13971400
force {bool, optional}: Force an update statement to be executed even if nothing was changed
13981401
cast {bool, optional}: Run all values through model's casters
1402+
ignore_mass_assignment {bool, optional}: Whether the update should ignore mass assignment on the model
13991403
14001404
Returns:
14011405
self
@@ -1407,7 +1411,8 @@ def update(
14071411
if self._model:
14081412
model = self._model
14091413
# Filter __fillable/__guarded__ fields
1410-
updates = model.filter_mass_assignment(updates)
1414+
if not ignore_mass_assignment:
1415+
updates = model.filter_mass_assignment(updates)
14111416

14121417
if model and model.is_loaded():
14131418
self.where(model.get_primary_key(), model.get_primary_key_value())

0 commit comments

Comments
 (0)