I am working with Node.js and MySQL and I am pretty new at this. I tried setting up a table with one column that accepts JSON data. I did this because I have a parent table and child table type of format and the parents would be able to hold multiple child_ids.
CREATE TABLE IF NOT EXISTS parents(
parent_id int PRIMARY KEY auto_increment,
first_name VARCHAR(255),
middle_name VARCHAR(255),
last_name VARCHAR(255),
child_ids JSON
);
I use this query to insert data into the parents table:
connection.query(`INSERT INTO parents (first_name, middle_name, last_name, child_ids) VALUES ('John', 'Malcom', 'Smith', '{"child_1": 129314812, "child_2": 18436987}');`, function(err, results, fields){
if (err) console.log(err.message);
});
However, I get this error, which I can't seem to find anything about:
ER_JSON_USED_AS_KEY: JSON column 'parent_ids' supports indexing only via generated columns on a specified JSON path.
If anyone knows about this please help me out!
EDIT: I commented this command out and I don't get the error anymore:
ALTER TABLE childs ADD FOREIGN KEY(parent_ids) REFERENCES parents(parent_id);
But how would I make it so that i can add the foreign key?