I have a table which links together two identifiers. I want these to be unique, i.e.:

```
(1, 2)
(1, 3)
(2, 1) <----- invalid, violates 1,2

```

I tried the following:

```
CREATE TABLE identifiers_relationships (
    id serial primary key,
    identifier1_id integer,
    identifier2_id integer,

    check (
        identifier1_id != identifier2_id
    )
);

CREATE OR REPLACE RULE identifiers_relationships_duplicate_rule AS 
    ON INSERT TO identifiers_relationships 
    WHERE EXISTS 
    (
        SELECT * FROM identifiers_relationships WHERE
        /* basically check for (a, b) already existing) */
        (identifier1_id = NEW.identifier1_id AND identifier2_id = NEW.identifier2_id)
        OR
        /* OR check if (b, a) exists */
        (identifier1_id = NEW.identifier2_id AND identifier2_id = NEW.identifier1_id)
    )
    DO NOTHING;

```

But then it still lets me do this:

```
db=> select * from identifiers_relationships;
 id | identifier1_id | identifier2_id 
----+----------------+----------------
  1 |              2 |              1
  2 |              1 |              2
(2 rows)

```