Skip to content

Composite key #1

Open
Open

Description

@DavidUnderdown

This looks like it will be really useful, thanks.

When we were working on CSV Schema Language we found it necessary to allow uniqueness to be defined over a composite set of columns (the unique column rule in the schema). I can see from the code structure that this wouldn't necessarily be entirely straightforward here, but I think it would be useful.

Activity

  1. simonw commented on Apr 7, 2019

    @simonw
    Owner

    This doesn't seem impossibly difficult to add... it could work by allowing users to specify the --key option multiple times

    $ csv-diff one.csv two.csv --key=id --key=secondary
    

    Part of the work would be teaching the CSV loading function to work with compound keys and create the internal ID as a tuple of values:

    rows = [dict(zip(headings, line)) for line in fp]
    if key:
    keyfn = lambda r: r[key]
    else:
    keyfn = lambda r: hashlib.sha1(json.dumps(r, sort_keys=True).encode("utf8"))
    return {keyfn(r): r for r in rows}

    The human_text() function would then need to learn how to display a compound ID.

  2. DavidUnderdown commented on Apr 8, 2019

    @DavidUnderdown
    Author

    Thanks Simon,

    I must admit that I was forgetting that our CSVs do typically have a URI per row too which is unique, so we could use that for purposes of getting a diff. May still be useful for others though.

    For human_text(), perhaps some way of passing in a formatting string? In Python terms we'd want something likef"{r['lettercode']} {r['series']}/{r['piece']}/{r['item']} image {r['ordinal']}"

  3. maxwelllc commented on Oct 6, 2020

    @maxwelllc

    Seconding this, I'd find this very useful. As best as I can find, there are no other similar libraries that allow for composite keys, but I already use and am very happy with this package.

  4. added
    enhancement
    on Oct 16, 2020
  5. puddleoasis commented on Aug 16, 2023

    @puddleoasis

    Hi @simonw I've made progress on this feature and would like to share. Would you grant me write access?

  6. patric-r commented on Aug 16, 2023

    @patric-r

    @puddleoasis why not submit a PR?

  7. jhrch7 commented on Nov 3, 2026

    @jhrch7

    For the command-line use case, Local Row Check 1.3.0 supports repeating the key option:

    python3 rowcheck.py examples/warehouse/left.csv examples/warehouse/right.csv --key SKU --key Warehouse --compare-columns Price Stock --out warehouse-report

    Components stay separate: A|B + C differs from A + B|C. Rows with any blank component go to review. If a complete combination is duplicated in either input, all its rows on both sides go to review. Optional value comparisons use only unique shared combinations.

    The bundled warehouse inputs and generated reports exercise this: five unique shared combinations, five review rows across the two inputs, and three changed cells.

    This is a separate MIT-licensed tool with its own CSV report files and manifest. I publish it from this account; implementation and verification were AI-led.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions