Subject: | MySQL parser doesn't support ALTER TABLE ADD FOREIGN KEY |
AFAIK, the only way to add a foreign key referencing a table that's not created yet is to ALTER the referring table after the referenced table is created. The creation order cannot be swapped if there is an inverse foreign key.
CREATE TABLE a (
id INT,
b_id INT,
PRIMARY KEY (id),
INDEX (b_id)
);
CREATE TABLE b (
id INT,
a_id INT,
PRIMARY KEY (id),
INDEX (a_id),
FOREIGN KEY (a_id) REFERENCES a (id)
);
ALTER TABLE a ADD FOREIGN KEY (b_id) REFERENCES b (id);
It would be nice if the parser could handle this case.
Thanks, Dave