mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 09:24:31 +02:00
Ability to create unique indexes, refs #14
This commit is contained in:
parent
1b6025e8ab
commit
e7ffbcdb36
3 changed files with 31 additions and 3 deletions
|
|
@ -236,16 +236,19 @@ class Table:
|
|||
self.exists = True
|
||||
return self
|
||||
|
||||
def create_index(self, columns, index_name=None):
|
||||
def create_index(self, columns, index_name=None, unique=False):
|
||||
if index_name is None:
|
||||
index_name = "idx_{}_{}".format(
|
||||
self.name.replace(" ", "_"), "_".join(columns)
|
||||
)
|
||||
sql = """
|
||||
CREATE INDEX {index_name}
|
||||
CREATE {unique}INDEX {index_name}
|
||||
ON {table_name} ({columns});
|
||||
""".format(
|
||||
index_name=index_name, table_name=self.name, columns=", ".join(columns)
|
||||
index_name=index_name,
|
||||
table_name=self.name,
|
||||
columns=", ".join(columns),
|
||||
unique="UNIQUE " if unique else "",
|
||||
)
|
||||
self.db.conn.execute(sql)
|
||||
return self
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue