From b436bdb594fad3134ce6eba2219809faf1472c6e Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Mon, 23 Mar 2020 13:31:06 -0700 Subject: [PATCH] Fixed bug with null columns, closes #95 --- sqlite_utils/utils.py | 11 ++++++++--- tests/test_create.py | 6 ++++++ tests/test_suggest_column_types.py | 1 + 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/sqlite_utils/utils.py b/sqlite_utils/utils.py index d086ddb..ecb3568 100644 --- a/sqlite_utils/utils.py +++ b/sqlite_utils/utils.py @@ -13,11 +13,16 @@ def suggest_column_types(records): all_column_types = {} for record in records: for key, value in record.items(): - if value is not None: - all_column_types.setdefault(key, set()).add(type(value)) + all_column_types.setdefault(key, set()).add(type(value)) column_types = {} + for key, types in all_column_types.items(): - if len(types) == 1: + # Ignore null values if at least one other type present: + if len(types) > 1: + types.discard(None.__class__) + if {None.__class__} == types: + t = str + elif len(types) == 1: t = list(types)[0] # But if it's a subclass of list / tuple / dict, use str # instead as we will be storing it as JSON in the table diff --git a/tests/test_create.py b/tests/test_create.py index 92e0f8a..905f49d 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -813,3 +813,9 @@ def test_insert_all_empty_list(fresh_db): assert 1 == fresh_db["t"].count fresh_db["t"].insert_all([], replace=True) assert 1 == fresh_db["t"].count + + +def test_create_with_a_null_column(fresh_db): + record = {"name": "Name", "description": None} + fresh_db["t"].insert(record) + assert [record] == list(fresh_db["t"].rows) diff --git a/tests/test_suggest_column_types.py b/tests/test_suggest_column_types.py index a392c09..e36c58f 100644 --- a/tests/test_suggest_column_types.py +++ b/tests/test_suggest_column_types.py @@ -21,6 +21,7 @@ from sqlite_utils.utils import suggest_column_types ([{"a": 1}, {"a": 1.1}], {"a": float}), ([{"a": b"b"}], {"a": bytes}), ([{"a": b"b"}, {"a": None}], {"a": bytes}), + ([{"a": "a", "b": None}], {"a": str, "b": str}), ], ) def test_suggest_column_types(records, types):