I have a unique restraint on a combination of columns, rather than on a single one. Naturally, MySQL errors if I then try to insert data that violates this restraint.
This is via PHP > CodeIgniter, by the way.
My question is: on inesrt, what is the suggested way of handling this? Should I...
- be suppressing the error? (#flameme)
- be running a query first to ensure it doesn't happen?
- allow the error because it doesn't cause any visible issue to the user?
- be using
replace into
rather thaninsert into
? (I believe CodeIgniter's Active Record class doesn't provide access toreplace into
, though?)
Context:
I have a tags table, which stores tags that users create to categorise their content:
- id (PK, int, AI, unsigned)
- tag (varchar 25)
- user_id (FK, int, unsigned)
A given user is not able create a tag he has previously created, hence the unique restraint is on the combination of the id and user_id columns.