My website allows people to become “friends”.
Once Person-A initiates a “Friend Request” with Person-B, then Person-A cannot make that same request again with Person-B.
Likewise, Person-B cannot turn around and make a “Friend Request” with Person-A. (Either scenario would be redundant!)
My code prevents these scenarios from happening, but ideally I would like to also enforce these business rules in MySQL.
Here is my FRIEND table…
- id (UK)
- requestor (PK1)(FK)
- requestee (PK2)(FK)
- requestor_approved
- requestee_approved
Let’s say that the following Friend-Request record already exists…
id requestor requestee requestor_approved requestoee_approved
--- ---------- ---------- ------------------- --------------------
1 19 2 0 0
Is there a way to prevent both of the following INSERTS into the database?
INSERT INTO friend (requestor, requestee, requestor_approved, requestee_approved)
VALUES (19, 2, 0, 0)
INSERT INTO friend (requestor, requestee, requestor_approved, requestee_approved)
VALUES (2, 19, 0, 0)
My Composite Primary Key seems to prevent the 1st INSERT from happening, however, unfortunately the 2nd INSERT zooms through.
Here is hoping there is a way in MySQL to prevent both INSERTS… :-/
Sincerely,
Debbie