I have a child table whose rows are related to various parent tables by parent_table_name, and parent_id columns.
Can I set up a foreign key constraint for these relationships?
In the tables below, I can run queries such as SELECT * FROM parent_1 p1 JOIN child c ON p1.id = c.parent_id AND c.parent_table_name = 'parent_1'; to get a row from parent_1 and any related rows from child.
CREATE TABLE parent_1 (
id INT NOT NULL AUTO_INCREMENT
, PRIMARY KEY(id)
);
INSERT INTO parent_1 VALUES(1);
CREATE TABLE parent_2 (
id INT NOT NULL AUTO_INCREMENT
, PRIMARY KEY(id)
);
INSERT INTO parent_2 VALUES(1);
CREATE TABLE child (
id INT NOT NULL AUTO_INCREMENT
, parent_table_name VARCHAR(16) NOT NULL
, parent_id INT NOT NULL
, some_interesting_value VARCHAR(255) NOT NULL
, PRIMARY KEY(id)
, KEY (parent_table_name, parent_id)
);
INSERT INTO child VALUES(NULL, 'parent_1', 1, 'Hello world');
INSERT INTO child VALUES(NULL, 'parent_2', 1, 'Goodbye world');
I have tried adding a constraint as below, but got a 1064 error.
ALTER TABLE parent_1 ADD CONSTRAINT p1_children FOREIGN KEY ("parent_1", id)
REFERENCES child (parent_table_name, parent_id);
ERROR 1064 (42000): You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near '"parent_1", id) REFERENCES
child (parent_table_name, parent_id)' at line 1
Is there some way to set up this sort of constraint? Or is the only way to have a third table that has the primary keys from the parent and child table?