diff --git a/cc.py b/cc.py index 44b2a5e4..5193c8e1 100644 --- a/cc.py +++ b/cc.py @@ -16,8 +16,9 @@ # print(builder.where("id", 1).or_where(lambda q: q.where('id', 2).or_where('id', 3)).get()) class User(Model): - __connection__ = "sqlite" + __connection__ = "mysql" __table__ = "users" + __dates__ = ["verified_at"] @has_many("id", "user_id") def articles(self): @@ -28,7 +29,9 @@ class Article(Model): # user = User.create({"name": "phill", "email": "phill"}) # print(inspect.isclass(User)) -print(User.find(1).with_("articles").first().serialize()) +user = User.first() +user.update({"verified_at": None, "updated_at": None}) +print(user.first().serialize()) # print(user.serialize()) # print(User.first()) \ No newline at end of file diff --git a/src/masoniteorm/query/QueryBuilder.py b/src/masoniteorm/query/QueryBuilder.py index 3a45a614..52f81b0a 100644 --- a/src/masoniteorm/query/QueryBuilder.py +++ b/src/masoniteorm/query/QueryBuilder.py @@ -1441,10 +1441,16 @@ def update( date_fields = model.get_dates() for key, value in updates.items(): if key in date_fields: - updates[key] = model.get_new_datetime_string(value) + if value: + updates[key] = model.get_new_datetime_string(value) + else: + updates[key] = value # Cast value if necessary if cast: - updates[key] = model.cast_value(value) + if value: + updates[key] = model.cast_value(value) + else: + updates[key] = value elif not updates: # Do not perform query if there are no updates return self diff --git a/src/masoniteorm/scopes/TimeStampsScope.py b/src/masoniteorm/scopes/TimeStampsScope.py index 5e4551cc..e9da387e 100644 --- a/src/masoniteorm/scopes/TimeStampsScope.py +++ b/src/masoniteorm/scopes/TimeStampsScope.py @@ -35,6 +35,9 @@ def set_timestamp_update(self, builder): if not builder._model.__timestamps__: return builder + for update in builder._updates: + if builder._model.date_updated_at in update.column: + return builder._updates += ( UpdateQueryExpression( { diff --git a/tests/postgres/grammar/test_update_grammar.py b/tests/postgres/grammar/test_update_grammar.py index 15a70b62..76d19f7f 100644 --- a/tests/postgres/grammar/test_update_grammar.py +++ b/tests/postgres/grammar/test_update_grammar.py @@ -71,6 +71,14 @@ def test_raw_expression(self): self.assertEqual(to_sql, sql) + def test_update_null(self): + to_sql = self.builder.update({"name": None}, dry=True).to_sql() + print(to_sql) + + sql = """UPDATE "users" SET "name" = \'None\'""" + + self.assertEqual(to_sql, sql) + class TestPostgresUpdateGrammar(BaseTestCaseUpdateGrammar, unittest.TestCase): grammar = "postgres"