In a relational database, foreign keys are normally used to associate records stored in different tables, but wouldn’t it be nice to define relationships dynamically without having to add extra columns or tables? And while we’re at it, how about having sparse relationships by associating a record directly with any other record like “post X was last edited by user #123” or “post X was flagged for review by user #456” (who happens to be a moderator)?

  • Kache
    link
    fedilink
    arrow-up
    9
    ·
    2 months ago

    So… a polymorphic many-to-many join table?

    • Aijan@programming.devOP
      link
      fedilink
      arrow-up
      3
      ·
      2 months ago

      Yes, that’s correct. Here’s how an entry in the join table looks like:

      {
        "id": 6,
        "sourceComp": "user",
        "sourceId": 2,
        "targetComp": "post",
        "targetId": 3,
        "type": "author",
        "createdAt": "2024-03-28T13:28:59.175Z",
        "updatedAt": "2024-03-28T13:28:59.175Z"
      }
      
      • Kache
        link
        fedilink
        arrow-up
        6
        ·
        edit-2
        2 months ago

        Fine for prototyping, but adds a scaling tech debt “time bomb” for a live system. Those associations had better be really sparse.

        • Aijan@programming.devOP
          link
          fedilink
          arrow-up
          2
          ·
          2 months ago

          There’s certainly the danger of creating too many ad-hoc or sparse relationships, which can cause issues. That said, when used for supplementing foreign keys, Tie-in can be a useful tool in a production system as well.