cldf / pycldf

python package to read and write CLDF datasets
https://cldf.clld.org
Apache License 2.0
15 stars 7 forks source link

Getting the target of a foreign key #133

Closed Anaphory closed 3 years ago

Anaphory commented 3 years ago

I have just tried to update some script to impement what we clarified in the preparation for 1.1, that explicit foreign keys have precedence and do not need to point to ID columns. The best way I could come up with (commas for tuple unpacking, because all the RHSs of the assigment are length-1 lists)

                form_column = col_map.cognates.formReference
                foreign_key, = [
                    key for key in ds["CognateTable"].tableSchema.foreignKeys
                    if key.columnReference == [form_column]
                ]
                target, = foreign_key.reference.columnReference

That's quite a chunk for something that should be a core operation. Did I overlook something? If not, could we get something better, such as a property on the column object?

xrotwang commented 3 years ago

Property on the column object wouldn't work, because the column doesn't know about the table(Schema) it's part of. So, something like Dataset.foreign_key_target(col) would be the best I can think of now. This should return a pair (Table, Column), though, I think.

xrotwang commented 3 years ago

Oops, so that should have been:

class Dataset:
    ...
    def foreign_key_target(self, table, col):
        return table, column
    ...

i.e. we also need to know the table of the foreign key.

xrotwang commented 3 years ago

And yes, considering the cognitive dissonance that we need to look up column names in columnReference, and not CLDF terms, that should be made simpler with such a method.