Skip to content
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

Utilities for building lookup tables #44

Closed
simonw opened this issue Jul 23, 2019 · 2 comments
Closed

Utilities for building lookup tables #44

simonw opened this issue Jul 23, 2019 · 2 comments
Labels
enhancement New feature or request

Comments

@simonw
Copy link
Owner

simonw commented Jul 23, 2019

While building https://github.com/dogsheep/healthkit-to-sqlite I found a need for a neat mechanism for easily building lookup tables - tables where each unique value in a column is replaced by a foreign key to a separate table.

csvs-to-sqlite currently creates those with its "extract" mechanism - but that's written as custom code against Pandas. I'd like to eventually replace Pandas with sqlite-utils there.

See also #42

@simonw simonw added the enhancement New feature or request label Jul 23, 2019
@simonw
Copy link
Owner Author

simonw commented Jul 23, 2019

id = table.lookup({"name":"Cleo"})

  • If table does not exist, create it with id, name where name is unique
  • If table does exist, add unique name column
  • If table and column exist, add unique constraint - throw error if impossible
  • now either insert the new row or return the existing ID

@simonw
Copy link
Owner Author

simonw commented Jul 23, 2019

I considered keyword arguments for this, but I am going with a dictionary instead - for two reasons:

  • leaves the option to add extra keyword arguments for further options later
  • supports column names that are not valid keyword arguments

simonw added a commit that referenced this issue Jul 23, 2019
@simonw simonw closed this as completed in 5805024 Jul 23, 2019
simonw added a commit that referenced this issue Jul 24, 2019
* Add pk column if missing from insert
* Implemented table.lookup(...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant