diff --git a/piccolo/utils/objects.py b/piccolo/utils/objects.py index 998b869e7..2f17c29a2 100644 --- a/piccolo/utils/objects.py +++ b/piccolo/utils/objects.py @@ -69,7 +69,7 @@ def make_nested_object( else: # The value doesn't belong to a foreign key, so just append it. table_params[key] = value - elif load_json and key in json_column_names: + elif load_json and key in json_column_names and value is not None: table_params[key] = encoding.load_json(value) else: table_params[key] = value diff --git a/tests/example_apps/music/piccolo_migrations/2021-07-25T22-38-48-009306.py b/tests/example_apps/music/piccolo_migrations/2021-07-25T22-38-48-009306.py index 0bddaf7cf..6da98adee 100644 --- a/tests/example_apps/music/piccolo_migrations/2021-07-25T22-38-48-009306.py +++ b/tests/example_apps/music/piccolo_migrations/2021-07-25T22-38-48-009306.py @@ -43,7 +43,7 @@ async def forwards(): column_class=JSON, params={ "default": "{}", - "null": False, + "null": True, "primary_key": False, "unique": False, "index": False, @@ -60,7 +60,7 @@ async def forwards(): column_class=JSONB, params={ "default": "{}", - "null": False, + "null": True, "primary_key": False, "unique": False, "index": False, diff --git a/tests/example_apps/music/tables.py b/tests/example_apps/music/tables.py index e8b5b856b..7d868bd9c 100644 --- a/tests/example_apps/music/tables.py +++ b/tests/example_apps/music/tables.py @@ -114,8 +114,8 @@ class RecordingStudio(Table): """ id: Serial - facilities = JSON() - facilities_b = JSONB() + facilities = JSON(null=True) + facilities_b = JSONB(null=True) class Instrument(Table): diff --git a/tests/table/test_join.py b/tests/table/test_join.py index 0aa31a676..f1ac36451 100644 --- a/tests/table/test_join.py +++ b/tests/table/test_join.py @@ -433,6 +433,32 @@ def test_objects_nested_with_load_json(self): {"restaurant": True}, ) + def test_objects_nested_with_load_json_null(self): + """ + Make sure that nested objects works alongside ``load_json``, when + the nested object has a null value for a JSON column. + + https://github.com/piccolo-orm/piccolo/issues/1391 + + """ + RecordingStudio.update( + { + RecordingStudio.facilities: None, + }, + force=True, + ).run_sync() + + instrument = ( + Instrument.objects(Instrument.recording_studio) + .output(load_json=True) + .first() + .run_sync() + ) + assert instrument is not None + self.assertIsNone( + instrument.recording_studio.facilities, + ) + def test_objects_prefetch_clause(self): """ Make sure that ``prefetch`` clause works correctly.