I am trying to validate a JSON column given a schema on a different table. Since a foreign key constraint is also set I can guarantee that a schema is assigned.
The following sql code is not valid on MySQL, however it should translate what I am attempting to solve:
CREATE TABLE modules(
id INTEGER NOT NULL PRIMARY KEY,
schema JSON NOT NULL
);
CREATE TABLE licenses(
id INTEGER NOT NULL PRIMARY KEY,
config JSON NOT NULL,
module_id INTEGER NOT NULL,
FOREIGN KEY (module_id) REFERENCES modules(id),
CHECK(JSON_SCHEMA_VALID(SELECT schema FROM modules WHERE id = module_id, config))
);
Is it possible to achieve this behavior on MySQL?