Skip to content

Commit c11fcc4

Browse files
Merge pull request #868 from MasoniteFramework/fix/816
Fixed date not being able to be set to null
2 parents 98a6393 + ea7f06e commit c11fcc4

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

cc.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
# print(builder.where("id", 1).or_where(lambda q: q.where('id', 2).or_where('id', 3)).get())
1717

1818
class User(Model):
19-
__connection__ = "sqlite"
19+
__connection__ = "mysql"
2020
__table__ = "users"
21+
__dates__ = ["verified_at"]
2122

2223
@has_many("id", "user_id")
2324
def articles(self):
@@ -28,7 +29,9 @@ class Article(Model):
2829

2930
# user = User.create({"name": "phill", "email": "phill"})
3031
# print(inspect.isclass(User))
31-
print(User.find(1).with_("articles").first().serialize())
32+
user = User.first()
33+
user.update({"verified_at": None, "updated_at": None})
34+
print(user.first().serialize())
3235

3336
# print(user.serialize())
3437
# print(User.first())

src/masoniteorm/query/QueryBuilder.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1441,10 +1441,16 @@ def update(
14411441
date_fields = model.get_dates()
14421442
for key, value in updates.items():
14431443
if key in date_fields:
1444-
updates[key] = model.get_new_datetime_string(value)
1444+
if value:
1445+
updates[key] = model.get_new_datetime_string(value)
1446+
else:
1447+
updates[key] = value
14451448
# Cast value if necessary
14461449
if cast:
1447-
updates[key] = model.cast_value(value)
1450+
if value:
1451+
updates[key] = model.cast_value(value)
1452+
else:
1453+
updates[key] = value
14481454
elif not updates:
14491455
# Do not perform query if there are no updates
14501456
return self

src/masoniteorm/scopes/TimeStampsScope.py

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ def set_timestamp_update(self, builder):
3535
if not builder._model.__timestamps__:
3636
return builder
3737

38+
for update in builder._updates:
39+
if builder._model.date_updated_at in update.column:
40+
return
3841
builder._updates += (
3942
UpdateQueryExpression(
4043
{

tests/postgres/grammar/test_update_grammar.py

+8
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ def test_raw_expression(self):
7171

7272
self.assertEqual(to_sql, sql)
7373

74+
def test_update_null(self):
75+
to_sql = self.builder.update({"name": None}, dry=True).to_sql()
76+
print(to_sql)
77+
78+
sql = """UPDATE "users" SET "name" = \'None\'"""
79+
80+
self.assertEqual(to_sql, sql)
81+
7482

7583
class TestPostgresUpdateGrammar(BaseTestCaseUpdateGrammar, unittest.TestCase):
7684
grammar = "postgres"

0 commit comments

Comments
 (0)