From 6f3cb2c106ae99f0a14201e6b4c61ec2f492e766 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Mon, 10 Feb 2020 21:13:15 -0800 Subject: [PATCH] create_index now works with columns with spaces, closes #85 --- sqlite_utils/db.py | 6 +++--- tests/test_create.py | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index 1cd19a0..0d23e0b 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -625,12 +625,12 @@ class Table(Queryable): self.name.replace(" ", "_"), "_".join(columns) ) sql = """ - CREATE {unique}INDEX {if_not_exists}{index_name} - ON {table_name} ({columns}); + CREATE {unique}INDEX {if_not_exists}[{index_name}] + ON [{table_name}] ({columns}); """.format( index_name=index_name, table_name=self.name, - columns=", ".join(columns), + columns=", ".join("[{}]".format(c) for c in columns), unique="UNIQUE " if unique else "", if_not_exists="IF NOT EXISTS " if if_not_exists else "", ) diff --git a/tests/test_create.py b/tests/test_create.py index bfa352f..15e73cf 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -540,27 +540,27 @@ def test_error_if_more_than_999_columns(fresh_db, num_columns, should_error): "columns,index_name,expected_index", ( ( - ["is_good_dog"], + ["is good dog"], None, Index( seq=0, - name="idx_dogs_is_good_dog", + name="idx_dogs_is good dog", unique=0, origin="c", partial=0, - columns=["is_good_dog"], + columns=["is good dog"], ), ), ( - ["is_good_dog", "age"], + ["is good dog", "age"], None, Index( seq=0, - name="idx_dogs_is_good_dog_age", + name="idx_dogs_is good dog_age", unique=0, origin="c", partial=0, - columns=["is_good_dog", "age"], + columns=["is good dog", "age"], ), ), ( @@ -579,7 +579,7 @@ def test_error_if_more_than_999_columns(fresh_db, num_columns, should_error): ) def test_create_index(fresh_db, columns, index_name, expected_index): dogs = fresh_db["dogs"] - dogs.insert({"name": "Cleo", "twitter": "cleopaws", "age": 3, "is_good_dog": True}) + dogs.insert({"name": "Cleo", "twitter": "cleopaws", "age": 3, "is good dog": True}) assert [] == dogs.indexes dogs.create_index(columns, index_name) assert expected_index == dogs.indexes[0]