From 1c34e1155c62512c37e5edab5e9b512fd33fa01b Mon Sep 17 00:00:00 2001 From: Aevann Date: Wed, 23 Oct 2024 23:01:09 +0300 Subject: [PATCH] add unique constraint --- migrations/20241022-cleanup-videos.sql | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/migrations/20241022-cleanup-videos.sql b/migrations/20241022-cleanup-videos.sql index 1221cfd97..1411e3a4c 100644 --- a/migrations/20241022-cleanup-videos.sql +++ b/migrations/20241022-cleanup-videos.sql @@ -1,6 +1,6 @@ create table media_usages ( - id integer primary key, - filename character varying(55) NOT NULL, + id integer primary key, + filename character varying(55) NOT NULL, post_id integer, comment_id integer, created_utc integer not null, @@ -9,12 +9,15 @@ create table media_usages ( ); CREATE SEQUENCE public.media_usages_id_seq - AS integer - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + +ALTER TABLE ONLY public.media_usages + ADD CONSTRAINT media_usages_unique UNIQUE NULLS NOT DISTINCT (filename, post_id, comment_id); ALTER SEQUENCE public.media_usages_id_seq OWNED BY public.media_usages.id; @@ -22,7 +25,7 @@ ALTER TABLE ONLY public.media_usages ALTER COLUMN id SET DEFAULT nextval('public alter table media_usages - add constraint media_usages_post_fkey foreign key (post_id) references posts(id); + add constraint media_usages_post_fkey foreign key (post_id) references posts(id); alter table media_usages - add constraint media_usages_comment_fkey foreign key (comment_id) references comments(id); \ No newline at end of file + add constraint media_usages_comment_fkey foreign key (comment_id) references comments(id); \ No newline at end of file