il.add_index question

I have a table that is modeling a friendship between two players. I'd
like it so that there can only be one record for this friendship in
the table. I have added an index to the table to make sure that a
record is unique like so ...

id1 id2
1 2
1 2 # Error, record isn't unique

add_index(:friendships, [:friend_id1, :friend_id2], :unique => true)

I would also like to add an index to take care of the following

id1 id2
1 2
2 1 # Error, since it's the same as the first record

How would I do this?

Could you setup your code so that for a friendship record id1 is
always less than id2? If there is no way in the code that id1=2 and
id2=1 can occur then you do not need to protect against it.


I see what your saying yeah, that could work. Before I add it to the
table, I would get it in the form id1 < id2 & then my previous index
would work the same way essentially catching both of the cases I was

Thanks for the help.

Yeah that’s true, thanks.