Skip to content

Instantly share code, notes, and snippets.

@sam0x17
Created January 24, 2017 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sam0x17/488f82a96c4acf39a615dab6c569b66e to your computer and use it in GitHub Desktop.
Save sam0x17/488f82a96c4acf39a615dab6c569b66e to your computer and use it in GitHub Desktop.
model flattening

The problem

CRM data is hierarchical, but RDBMSes are not. In a hierarchical system, deleting is as easy as deleting a single node. In an RDBMS, especially one with CRM data, this can be extremely difficult because there is a dense hierarchy of polymorphic relationships standing between you and the records you want to delete (or access!).

How to fix it

We need flat models!! This means that if there is a hierarchy of records, for example an Account has many Users, which each have many tasks, which in turn can have custom field values, then every single custom value record should have a task_id, a user_id, and an account_id ON THE RECORD. Even if custom field values are used on non-task tables, then the custom field values should simply have id's associated with those tables as well. For example if Projects can have custom field values, then there should also be a project_id field on each custom field value, even if it is usually set to null!

This will allow us to:

  • delete all records associated with any given record in a single query
  • traverse the hierarchy of records without performing any joins
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment