sqlite-utils
sqlite-utils copied to clipboard
Ability to customize columns used by extracts= feature
@simonw any thoughts on allow extracts to specify the lookup column name? If I'm understanding the documentation right, .lookup() allows you to define the "value" column (the documentation uses name), but when you use extracts keyword as part of .insert(), .upsert() etc. the lookup must be done against a column named "value". I have an existing lookup table that I've populated with columns "id" and "name" as opposed to "id" and "value", and seems I can't use extracts=, unless I'm missing something...
Initial thought on how to do this would be to allow the dictionary value to be a tuple of table name column pair... so:
table = db.table("trees", extracts={"species_id": ("Species", "name"})
I haven't dug too much into the existing code yet, but does this make sense? Worth doing?
Originally posted by @chrishas35 in https://github.com/simonw/sqlite-utils/issues/46#issuecomment-592999503
If you're happy with the proposed implementation, I have code & tests written that I'll get ready for a PR.
Yes pleas, I'd love to see that pull request!
I came up with potential syntax for that here: https://github.com/simonw/sqlite-utils/issues/49#issuecomment-710393550 - based on how table.extract(...) works:
fresh_db.table("tree", extracts=[Extract(
columns=("CommonName", "LatinName"),
table="Species",
fk_column="species_id",
rename={"CommonName": "name", "LatinName": "latin"}
)])