Skip to content

Commit

Permalink
Merge 1099629 into af2aab8
Browse files Browse the repository at this point in the history
  • Loading branch information
long2ice committed Dec 29, 2021
2 parents af2aab8 + 1099629 commit 12e7d8f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Changelog
------
Fixed
^^^^^
- Fix `bulk_create` error. (#1012)
- Fix `bulk_create` error. (#1012) (#1022)
- Fix unittest invalid.
- Fix `bulk_update` in `postgres` with some type like `datetime`. (#968)

Expand Down
4 changes: 2 additions & 2 deletions tests/contrib/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ async def test_json_field(self):
self.assertEqual(
ret0,
{
"id": 1,
"id": json_field_0.pk,
"data": {"a": 1},
"data_null": None,
"data_default": {"a": 1},
Expand All @@ -1026,7 +1026,7 @@ async def test_json_field(self):
self.assertEqual(
ret1,
{
"id": 2,
"id": json_field_1.pk,
"data": [{"a": 1, "b": 2}],
"data_null": None,
"data_default": {"a": 1},
Expand Down
12 changes: 12 additions & 0 deletions tests/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ async def test_bulk_update_pk_uuid(self):
self.assertEqual((await UUIDFields.get(pk=objs[0].pk)).data, objs[0].data)
self.assertEqual((await UUIDFields.get(pk=objs[1].pk)).data, objs[1].data)

async def test_bulk_update_json_value(self):
objs = [
await JSONFields.create(data={}),
await JSONFields.create(data={}),
]
objs[0].data = [0]
objs[1].data = {"a": 1}
rows_affected = await JSONFields.bulk_update(objs, fields=["data"])
self.assertEqual(rows_affected, 2)
self.assertEqual((await JSONFields.get(pk=objs[0].pk)).data, objs[0].data)
self.assertEqual((await JSONFields.get(pk=objs[1].pk)).data, objs[1].data)

async def test_update_auto_now(self):
obj = await DefaultUpdate.create()

Expand Down
6 changes: 6 additions & 0 deletions tortoise/filters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import operator
from datetime import datetime
from functools import partial
Expand Down Expand Up @@ -56,6 +57,11 @@ def get_value_sql(self, **kwargs): # pragma: nocoverage
if dialect == Dialects.POSTGRESQL.value:
return f"{value}::uuid"
return value
if isinstance(self.value, (dict, list)):
value = format_quotes(json.dumps(self.value), quote_char)
if dialect == Dialects.POSTGRESQL.value:
return f"{value}::jsonb"
return value
if isinstance(self.value, bool):
return str.lower(str(self.value))
if self.value is None:
Expand Down
2 changes: 1 addition & 1 deletion tortoise/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1683,7 +1683,7 @@ def _make_query(self) -> None:
for obj in objects_item:
value = executor.column_map[pk_attr](obj.pk, None)
field_value = getattr(obj, field)
case.when(pk == value, field_value)
case.when(pk == value, ValueWrapper(field_value))
pk_list.append(value)
query = query.set(field, case)
query = query.where(pk.isin(pk_list))
Expand Down

0 comments on commit 12e7d8f

Please sign in to comment.