Question

So I am trying to set up a trigger that when a new row to my user table is created a new row is also created in my profile table and the primary key in the user table is inserted into the foreign key slot in the profile table. This is the code I am using at the moment and it hasn't been working out.

    CREATE TRIGGER userID
AFTER INSERT ON user FOR EACH ROW
BEGIN
    INSERT INTO profile (userID)
    VALUES(user.userID);
END

The foreign key on the profile table and the primary on the user table are both called userID

No correct solution

OTHER TIPS

Saying user.userID doesn't mean anything useful in that context, you want to reference the newly created row using NEW:

CREATE TRIGGER userID
AFTER INSERT ON user FOR EACH ROW
BEGIN
    INSERT INTO profile (userID)
    VALUES(NEW.userID);
END

From the fine manual:

You can refer to columns in the subject table (the table associated with the trigger) by using the aliases OLD and NEW. [...] NEW.col_name refers to the column of a new row to be inserted or an existing row after it is updated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top