-- Add unique keyword_i18n locale constraint -- First we have to delete the duplicates that already exists (keywords 1316-1326; 138 keywords_i18n entries) with duplicates as ( select duplicate.id duplicate_id, * from keywords_i18n first -- inner join the same table where keyword_id, locale_id AND value must all be the same, -- i.e. only duplicate translations with the same translated value will be included join keywords_i18n duplicate using (keyword_id, locale_id, value) where first.id <> duplicate.id -- only include the last copy, so we dont delete the original and duplicate.id > first.id ) delete from keywords_i18n where id in (select duplicate_id from duplicates); -- Now we are allowed to add our constraint ALTER TABLE keywords_i18n ADD UNIQUE (keyword_id, locale_id);