pk=['id'] now equivalent to pk='id', closes #181

This commit is contained in:
Simon Willison 2020-10-14 14:59:38 -07:00
commit 7c0ef116ed
2 changed files with 8 additions and 5 deletions

View file

@ -330,6 +330,8 @@ class Database:
column_defs = []
# ensure pk is a tuple
single_pk = None
if isinstance(pk, list) and len(pk) == 1 and isinstance(pk[0], str):
pk = pk[0]
if isinstance(pk, str):
single_pk = pk
if pk not in [c[0] for c in column_items]:

View file

@ -73,11 +73,12 @@ def test_create_table_compound_primary_key(fresh_db):
assert ["id1", "id2"] == table.pks
def test_create_table_with_bad_defaults(fresh_db):
with pytest.raises(AssertionError):
fresh_db.create_table(
"players", {"name": str, "score": int}, defaults={"mouse": 1}
)
@pytest.mark.parametrize("pk", ("id", ["id"]))
def test_create_table_with_single_primary_key(fresh_db, pk):
fresh_db["foo"].insert({"id": 1}, pk=pk)
assert (
fresh_db["foo"].schema == "CREATE TABLE [foo] (\n [id] INTEGER PRIMARY KEY\n)"
)
def test_create_table_with_invalid_column_characters(fresh_db):