sqlite-utils icon indicating copy to clipboard operation
sqlite-utils copied to clipboard

Ability to customize columns used by extracts= feature

Open simonw opened this issue 5 years ago • 3 comments

@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

simonw avatar Mar 01 '20 16:03 simonw

If you're happy with the proposed implementation, I have code & tests written that I'll get ready for a PR.

chrishas35 avatar Mar 01 '20 17:03 chrishas35

Yes pleas, I'd love to see that pull request!

simonw avatar Apr 18 '20 00:04 simonw

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"}
)])

simonw avatar Oct 16 '20 19:10 simonw