I wish to add a unique constraint to a table using the following query -
ALTER TABLE events ADD CONSTRAINT events_timestamp_request_session_key UNIQUE (event_timestamp, request_id, session_id); The query is valid, however on one database the existing data does not meet the constraint, and so the query fails -
ERROR: could not create unique index "events_timestamp_request_session_key"
DETAIL: Key (event_timestamp, request_id, session_id)=(2017-07-05 14:53:25.475246+00, a55df6-8533e322cd-aa9d57-87e2, 132443) is duplicated.
It is expected that there would be a few duplicates, but unfortunately I cannot simply delete or alter them.
Is there any way to add the constraint as required, while ignoring the existing data in the table?