1

Let's say I have following table structure:

users:

id INT(10)

posts:

id INT(10)

users_posts:

user_id (FOREIGN KEY [fk_up_1]: users.id)
post_id (FOREIGN KEY [fk_up_2]: posts.id)

Then, I create a procedure like this:

CREATE PROCEDURE sp_user_add_post(v_user_id INT(10), v_post_id INT(10))
BEGIN
    DECLARE EXIT HANDLER FOR 1452 RESIGNAL SET MESSAGE_TEXT = "Invalid user_id or post_id";
    DECLARE EXIT HANDLER FOR SQLEXCEPTION RESIGNAL;

    INSERT INTO `users_posts` (`user_id`, `post_id`)
        VALUES (v_user_id, v_post_id);
END

The question: how can I differentiate between specific FK error conditions? Can I retrieve a name of FK that errored (fk_up_1 and fk_up_2 in my example above)? For instance, I might want to log an error somewhere if invalid user_id is passed, so it can come in handy.

4

0 回答 0