'Preventing multiple insertions of a connection between two users (SQL / SQLite)

I am currently working on a project where I want to store a connection between two users and some additional information in a SQLite database. The problem is that I want to store this connection exactly once and not have multiple entries for this connection.

So if I have userA and userB and for example the time this connection was made (userA, userB, time), I don't want to be able to store the reverse (userB, userA, time) as well.

My current solution is to sort the users and combine them into a single string, which is then used to create a primary key (userAuserB, userA, userB, time), preventing re-insertion. But I don't think this is the right way to do it.

Is there a better solution for this task?
Thanks, Ypselon.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source