Skip to content

Commit 23be5be

Browse files
committed
create-table now supports multiple --pk, refs #620
1 parent 1dc5da3 commit 23be5be

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

docs/cli.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,6 +1909,8 @@ This will create a table called ``mytable`` with two columns - an integer ``id``
19091909

19101910
You can pass as many column-name column-type pairs as you like. Valid types are ``integer``, ``text``, ``float`` and ``blob``.
19111911

1912+
Pass ``--pk`` more than once for a compound primary key that covers multiple columns.
1913+
19121914
You can specify columns that should be NOT NULL using ``--not-null colname``. You can specify default values for columns using ``--default colname defaultvalue``.
19131915

19141916
.. code-block:: bash

sqlite_utils/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,7 @@ def create_database(path, enable_wal, init_spatialite, load_extension):
14891489
)
14901490
@click.argument("table")
14911491
@click.argument("columns", nargs=-1, required=True)
1492-
@click.option("--pk", help="Column to use as primary key")
1492+
@click.option("pks", "--pk", help="Column to use as primary key", multiple=True)
14931493
@click.option(
14941494
"--not-null",
14951495
multiple=True,
@@ -1532,7 +1532,7 @@ def create_table(
15321532
path,
15331533
table,
15341534
columns,
1535-
pk,
1535+
pks,
15361536
not_null,
15371537
default,
15381538
fk,
@@ -1581,7 +1581,7 @@ def create_table(
15811581
)
15821582
db[table].create(
15831583
coltypes,
1584-
pk=pk,
1584+
pk=pks[0] if len(pks) == 1 else pks,
15851585
not_null=not_null,
15861586
defaults=dict(default),
15871587
foreign_keys=fk,

tests/test_cli.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,14 @@ def test_upsert_alter(db_path, tmpdir):
11801180
["age", "integer", "--default", "age", "3"],
11811181
("CREATE TABLE [t] (\n" " [age] INTEGER DEFAULT '3'\n" ")"),
11821182
),
1183+
# Compound primary key
1184+
(
1185+
["category", "text", "name", "text", "--pk", "category", "--pk", "name"],
1186+
(
1187+
"CREATE TABLE [t] (\n [category] TEXT,\n [name] TEXT,\n"
1188+
" PRIMARY KEY ([category], [name])\n)"
1189+
),
1190+
),
11831191
],
11841192
)
11851193
def test_create_table(args, schema):

0 commit comments

Comments
 (0)