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

sqlite-utils add-foreign-keys command #157

Closed
simonw opened this issue Sep 9, 2020 · 2 comments
Closed

sqlite-utils add-foreign-keys command #157

simonw opened this issue Sep 9, 2020 · 2 comments
Labels
cli-tool enhancement New feature or request
Milestone

Comments

@simonw
Copy link
Owner

simonw commented Sep 9, 2020

Like add-foreign-key but can do multiple foreign keys at once. Inspired by https://github.com/simonw/calands-datasette/blob/99de39dd80a906f5c1f16724467b0cd55ba4ef36/build.sh which does this:

sqlite-utils add-foreign-key calands.db units_with_maps ACCESS_TYP
sqlite-utils add-foreign-key calands.db units_with_maps AGNCY_NAME
sqlite-utils add-foreign-key calands.db units_with_maps AGNCY_LEV
sqlite-utils add-foreign-key calands.db units_with_maps AGNCY_TYP
sqlite-utils add-foreign-key calands.db units_with_maps LAYER
sqlite-utils add-foreign-key calands.db units_with_maps MNG_AGENCY
sqlite-utils add-foreign-key calands.db units_with_maps MNG_AG_LEV
sqlite-utils add-foreign-key calands.db units_with_maps MNG_AG_TYP
sqlite-utils add-foreign-key calands.db units_with_maps COUNTY
sqlite-utils add-foreign-key calands.db units_with_maps DES_TP
@simonw simonw added the enhancement New feature or request label Sep 9, 2020
@simonw
Copy link
Owner Author

simonw commented Sep 9, 2020

This can accept four arguments: table, column, other_table, other_column:

sqlite-utils add-foreign-keys calands.db \
  units_with_maps ACCESS_TYP ACCESS_TYP id \
  units_with_maps AGNCY_NAME AGNCY_NAME id \
  units_with_maps AGNCY_LEV AGNCY_LEV id

@simonw
Copy link
Owner Author

simonw commented Sep 9, 2020

It will call this method:

def add_foreign_keys(self, foreign_keys):
# foreign_keys is a list of explicit 4-tuples
assert all(
len(fk) == 4 and isinstance(fk, (list, tuple)) for fk in foreign_keys
), "foreign_keys must be a list of 4-tuples, (table, column, other_table, other_column)"
foreign_keys_to_create = []

@simonw simonw added this to the 2.19 milestone Sep 20, 2020
@simonw simonw closed this as completed in 3cc1944 Sep 20, 2020
simonw added a commit that referenced this issue Sep 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli-tool enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant