Skip to content

pk=["id"] should have same effect as pk="id" #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
simonw opened this issue Sep 28, 2020 · 1 comment
Closed

pk=["id"] should have same effect as pk="id" #181

simonw opened this issue Sep 28, 2020 · 1 comment
Labels
bug Something isn't working

Comments

@simonw
Copy link
Owner

simonw commented Sep 28, 2020

In [11]: db['one'].insert({"id": 1, "name": "oentuh"}, pk="id")
Out[11]: <Table one (id, name)>

In [12]: db['two'].insert({"id": 1, "name": "oentuh"}, pk=["id"])
Out[12]: <Table two (id, name)>

In [13]: db['one'].schema
Out[13]: 'CREATE TABLE [one] (\n   [id] INTEGER PRIMARY KEY,\n   [name] TEXT\n)'

In [14]: db['two'].schema
Out[14]: 'CREATE TABLE [two] (\n   [id] INTEGER,\n   [name] TEXT\n)'
@simonw simonw added the bug Something isn't working label Sep 28, 2020
@simonw
Copy link
Owner Author

simonw commented Sep 28, 2020

Relevant code:

# ensure pk is a tuple
single_pk = None
if isinstance(pk, str):
single_pk = pk
if pk not in [c[0] for c in column_items]:
column_items.insert(0, (pk, int))
for column_name, column_type in column_items:
column_extras = []
if column_name == single_pk:
column_extras.append("PRIMARY KEY")
if column_name in not_null:
column_extras.append("NOT NULL")
if column_name in defaults and defaults[column_name] is not None:
column_extras.append(
"DEFAULT {}".format(self.escape(defaults[column_name]))
)
if column_name in foreign_keys_by_column:
column_extras.append(
"REFERENCES [{other_table}]([{other_column}])".format(
other_table=foreign_keys_by_column[column_name].other_table,
other_column=foreign_keys_by_column[column_name].other_column,
)
)
column_defs.append(
" [{column_name}] {column_type}{column_extras}".format(
column_name=column_name,
column_type=COLUMN_TYPE_MAPPING[column_type],
column_extras=(" " + " ".join(column_extras))
if column_extras
else "",
)
)
extra_pk = ""
if single_pk is None and pk and len(pk) > 1:
extra_pk = ",\n PRIMARY KEY ({pks})".format(
pks=", ".join(["[{}]".format(p) for p in pk])
)

@simonw simonw closed this as completed in 7c0ef11 Oct 14, 2020
simonw added a commit that referenced this issue Oct 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant