From 8f042ae1fd323995d966a94e8e6df85cc843b938 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 14 Feb 2021 13:03:17 -0800 Subject: [PATCH] Fix for bug with extra columns in later chunks, closes #234 Thanks @nieuwenhoven for the fix, proposed in #225 --- sqlite_utils/db.py | 10 ++++------ tests/test_create.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index 17d79de..b6315dd 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -1904,12 +1904,10 @@ class Table(Queryable): if hash_id: all_columns.insert(0, hash_id) else: - all_columns += [ - column - for record in chunk - for column in record - if column not in all_columns - ] + for record in chunk: + all_columns += [ + column for column in record if column not in all_columns + ] validate_column_names(all_columns) first = False diff --git a/tests/test_create.py b/tests/test_create.py index bee47a7..2975dea 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -569,6 +569,22 @@ def test_insert_replace_rows_alter_table(fresh_db, use_table_factory): ] == list(table.rows) +def test_insert_all_with_extra_columns_in_later_chunks(fresh_db): + chunk = [ + {"record": "Record 1"}, + {"record": "Record 2"}, + {"record": "Record 3"}, + {"record": "Record 4", "extra": 1}, + ] + fresh_db["t"].insert_all(chunk, batch_size=2, alter=True) + assert list(fresh_db["t"].rows) == [ + {"record": "Record 1", "extra": None}, + {"record": "Record 2", "extra": None}, + {"record": "Record 3", "extra": None}, + {"record": "Record 4", "extra": 1}, + ] + + def test_bulk_insert_more_than_999_values(fresh_db): "Inserting 100 items with 11 columns should work" fresh_db["big"].insert_all(