Skip to content

Instantly share code, notes, and snippets.

@HristoKolev
Created February 29, 2024 10:31
Show Gist options
  • Save HristoKolev/991dbc200c86c2c3ab456d524c07d8aa to your computer and use it in GitHub Desktop.
Save HristoKolev/991dbc200c86c2c3ab456d524c07d8aa to your computer and use it in GitHub Desktop.
postgressql constraints fix
-- select all duplicate layouts
with agg as (SELECT name, type, owner FROM layouts GROUP BY name, type, owner HAVING COUNT(*) > 1)
SELECT
l.*
FROM agg
JOIN layouts l ON l.name = agg.name AND l.type = agg.type AND l.owner = agg.owner;
-- select all duplicate prefs
with agg as (SELECT app, "user" FROM prefs GROUP BY app, "user" HAVING COUNT(*) > 1)
SELECT
p.*
FROM agg
JOIN prefs p ON p.app = agg.app AND p.user = agg."user";
-- add layouts constraint
ALTER TABLE layouts
ADD CONSTRAINT layouts__name__type__owner__unique
UNIQUE ("name", "type", "owner");
-- add prefs constraint
ALTER TABLE prefs
ADD CONSTRAINT prefs__app__user__unique
UNIQUE ("app", "user");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment