Skip to content
This repository has been archived by the owner on Jan 4, 2024. It is now read-only.

Stringent unique constraints on time series data tables #17

Open
mactyr opened this issue Feb 8, 2016 · 2 comments
Open

Stringent unique constraints on time series data tables #17

mactyr opened this issue Feb 8, 2016 · 2 comments
Labels

Comments

@mactyr
Copy link
Contributor

mactyr commented Feb 8, 2016

As an energyPATHWAYS developer
I want tight constraints on my time series data tables
so that I don't accidentally set up conflicting or duplicate data while working on my models

AC

  1. All tables that store time-series data (e.g. SupplyTechsInstallationCostsNewData, SupplyTechsVariableMaintenanceCostData) should have a multi-column unique constraint that covers all columns except for "value". It would be wise to review the full list of constraints with @BenHaleyEvolvedEnergyResearch before implementing.

Future Work

This ticket is to document a particular class of helpful constraints that came up in discussion today. We should review other kinds of tables to see what additional constraints would be helpful as well.

@mactyr
Copy link
Contributor Author

mactyr commented Mar 30, 2016

It would be efficient to do this after we consolidate the database tables so we only have a few tables to add constraints to rather than many.

@mactyr
Copy link
Contributor Author

mactyr commented May 9, 2016

Note that by default postgres treats NULL values as always being unequal to each other (at least for the purposes of unique constraints) meaning that if you apply a unique constraint across several columns duplicates will be allowed as long as at least one of the columns is NULL in both rows! This is a common situation for us since, e.g., oth_1_id or oth_2_id will often be NULL. So a simple unique constraint won't do much good for many of our tables.

It is possible but not trivial to work around this using partial indexes or by coalescing NULLs to some non-null value in the index (-1 might work nicely for us since the things we are trying to enforce uniqueness on -- foreign keys, years, etc. -- generally won't be negative). See the first couple of answers here.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant