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 date not being able to be set to null #868

Merged
merged 5 commits into from
Jan 4, 2024
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
7 changes: 5 additions & 2 deletions cc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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())
10 changes: 8 additions & 2 deletions src/masoniteorm/query/QueryBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/masoniteorm/scopes/TimeStampsScope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
8 changes: 8 additions & 0 deletions tests/postgres/grammar/test_update_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down