The code below, when executed produces the following error:
Fatal error: Uncaught exception ‘PDOException’ with message ‘SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
$db->executeUpdate('CREATE TABLE submission_statuses (name VARCHAR(255) NOT NULL,
weight INT NOT NULL, PRIMARY KEY(name)) ENGINE = InnoDB');
$db->executeUpdate('ALTER TABLE submissions ADD CONSTRAINT IDX_4
FOREIGN KEY (status) REFERENCES submission_statuses(name)');
$db->executeUpdate('CREATE INDEX IDX_4 ON submissions (status)');
The tables both use the InnoDB engine.
The table
submissionscontains a value in thestatuscolumn that does not exist in the newly createdsubmission_statusestable. Thesubmission_statusestable must be populated before the constraint is created.