mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-24 18:04:32 +02:00
create_index(..., find_unique_name=True) option, refs #335
This commit is contained in:
parent
92aa5c9c5d
commit
e8d958109e
3 changed files with 62 additions and 18 deletions
|
|
@ -4,6 +4,7 @@ from sqlite_utils.db import (
|
|||
DescIndex,
|
||||
AlterError,
|
||||
NoObviousTable,
|
||||
OperationalError,
|
||||
ForeignKey,
|
||||
Table,
|
||||
View,
|
||||
|
|
@ -752,6 +753,22 @@ def test_create_index_desc(fresh_db):
|
|||
)
|
||||
|
||||
|
||||
def test_create_index_find_unique_name():
|
||||
db = Database(memory=True)
|
||||
table = db["t"]
|
||||
table.insert({"id": 1})
|
||||
table.create_index(["id"])
|
||||
# Without find_unique_name should error
|
||||
with pytest.raises(OperationalError, match="index idx_t_id already exists"):
|
||||
table.create_index(["id"])
|
||||
# With find_unique_name=True it should work
|
||||
table.create_index(["id"], find_unique_name=True)
|
||||
table.create_index(["id"], find_unique_name=True)
|
||||
# Should have three now
|
||||
index_names = {idx.name for idx in table.indexes}
|
||||
assert index_names == {"idx_t_id", "idx_t_id_2", "idx_t_id_3"}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"data_structure",
|
||||
(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue