Last active
December 14, 2015 22:19
-
-
Save rintaun/5157422 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+---------------+ | |
| organizations | | |
+---------------+ | |
+------>| id(PK) |<---------+ | |
| +---------------+ | | |
| | | |
| +--------+ +----------+ | | |
| | groups | | profiles | | | |
| +--------+ +----------+ | | |
+---| org_id | | org_id |---+ | |
+-->| id(PK) | | id(PK) |<--+ | |
| +--------+ +----------+ | | |
| | | |
| +----------------+ | | |
| | group_profiles | | | |
| +----------------+ | | |
+-------| group_id | | | |
| profile_id |---------+ | |
+----------------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the file
without_org_id_fk
, entries ingroup_profiles
cannot guarantee that the referencedgroup
andprofile
will be owned by the sameorganization
.In order to guarantee this,
group_profiles
must have anorg_id
attribute as well, and reference it in its relationships with bothgroups
andprofiles
.Currently, this requires an additional constraint --
UNIQUE (id, org_id)
-- on bothgroups
andprofiles
; without it PG complains:However, a set of attributes is necessarily unique if one or more of the attributes contained in that set is unique. Which is to say, the uniqueness of the keys is implied as the
id
attribute in each case is a PK.