diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index 0d23e0b..e781e2a 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -1,5 +1,5 @@ from .utils import sqlite3, OperationalError, suggest_column_types -from collections import namedtuple +from collections import namedtuple, OrderedDict import datetime import hashlib import itertools diff --git a/sqlite_utils/utils.py b/sqlite_utils/utils.py index 0645300..6da6d47 100644 --- a/sqlite_utils/utils.py +++ b/sqlite_utils/utils.py @@ -18,10 +18,11 @@ def suggest_column_types(records): for key, types in all_column_types.items(): if len(types) == 1: t = list(types)[0] - # But if it's list / tuple / dict, use str instead as we - # will be storing it as JSON in the table - if t in (list, tuple, dict): - t = str + # But if it's a subclass of list / tuple / dict, use str + # instead as we will be storing it as JSON in the table + for superclass in (list, tuple, dict): + if issubclass(t, superclass): + t = str elif {int, bool}.issuperset(types): t = int elif {int, float, bool}.issuperset(types): diff --git a/tests/test_create.py b/tests/test_create.py index 15e73cf..7f22ded 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -621,6 +621,7 @@ def test_create_index_if_not_exists(fresh_db): ["list with", "two items"], {"dictionary": "simple"}, {"dictionary": {"nested": "complex"}}, + collections.OrderedDict([("key1", {"nested": "complex"}), ("key2", "foo"),]), [{"list": "of"}, {"two": "dicts"}], ), ) diff --git a/tests/test_suggest_column_types.py b/tests/test_suggest_column_types.py index d9fe6ff..9427444 100644 --- a/tests/test_suggest_column_types.py +++ b/tests/test_suggest_column_types.py @@ -1,4 +1,5 @@ import pytest +from collections import OrderedDict from sqlite_utils.utils import suggest_column_types @@ -11,6 +12,7 @@ from sqlite_utils.utils import suggest_column_types ([{"a": [1]}], {"a": str}), ([{"a": (1,)}], {"a": str}), ([{"a": {"b": 1}}], {"a": str}), + ([{"a": OrderedDict({"b": 1})}], {"a": str}), ([{"a": 1}, {"a": 1.1}], {"a": float}), ([{"a": b"b"}], {"a": bytes}), ],