Skip to content

Document how to add a primary key to a rowid table using sqlite-utils transform --pk #403

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
fgregg opened this issue Feb 8, 2022 · 4 comments

Comments

@fgregg
Copy link
Contributor

fgregg commented Feb 8, 2022

Original title: Add option for adding a new, serial, primary key

sometimes we have tables that don't have primary keys, but ought to have them. we can use rowid for that, but it would often be nicer to have an explicit primary key. using the current value of rowid would be fine.

@fgregg
Copy link
Contributor Author

fgregg commented Feb 8, 2022

you can hack something like this to achieve this result:

sqlite-utils convert my_database my_table rowid "{'id': value}" --multi

@simonw
Copy link
Owner

simonw commented Feb 8, 2022

This is already possible using sqlite-utils transform like so:

% echo '[{"name": "Barry"}, {"name": "Sandra"}]' | sqlite-utils insert rowid.db records -
% sqlite-utils schema rowid.db                                                           
CREATE TABLE [records] (
   [name] TEXT
);
% sqlite-utils rows rowid.db records
[{"name": "Barry"},
 {"name": "Sandra"}]
% sqlite-utils transform rowid.db records --pk id
% sqlite-utils rows rowid.db records
[{"id": 1, "name": "Barry"},
 {"id": 2, "name": "Sandra"}]
% sqlite-utils schema rowid.db
CREATE TABLE "records" (
   [id] INTEGER PRIMARY KEY,
   [name] TEXT
);
% echo '[{"name": "Barry 2"}, {"name": "Sandra 2"}]' | sqlite-utils insert rowid.db records -
% sqlite-utils rows rowid.db records                                                         
[{"id": 1, "name": "Barry"},
 {"id": 2, "name": "Sandra"},
 {"id": 3, "name": "Barry 2"},
 {"id": 4, "name": "Sandra 2"}]

It's not covered in the documentation though: https://sqlite-utils.datasette.io/en/3.23/cli.html#transforming-tables

@simonw simonw changed the title add option for adding a new, serial, primary key Document how to add a primary key to a rowid table using sqlite-utils transform --pk Feb 8, 2022
@simonw simonw closed this as completed in 3acc2f1 Feb 8, 2022
@simonw
Copy link
Owner

simonw commented Feb 8, 2022

New documentation: https://sqlite-utils.datasette.io/en/latest/cli.html#adding-a-primary-key-to-a-rowid-table

@fgregg
Copy link
Contributor Author

fgregg commented Feb 9, 2022

dddoooope

simonw added a commit that referenced this issue Feb 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants