diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index c48483d..9ac8e23 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -224,6 +224,8 @@ class Database: single_pk = None if isinstance(pk, str): single_pk = pk + if pk not in [c[0] for c in column_items]: + column_items.insert(0, (pk, int)) for column_name, column_type in column_items: column_extras = [] if column_name == single_pk: diff --git a/tests/test_create.py b/tests/test_create.py index 288a8ee..222a967 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -734,3 +734,8 @@ def test_cannot_provide_both_filename_and_memory(): AssertionError, match="Either specify a filename_or_conn or pass memory=True" ): Database("/tmp/foo.db", memory=True) + + +def test_creates_id_column(fresh_db): + last_pk = fresh_db.table("cats", pk="id").insert({"name": "barry"}).last_pk + assert [{"name": "barry", "id": last_pk}] == list(fresh_db["cats"].rows)