0
Hello, I am doing a job that consists in developing a system to manage projects and etc. In this system I need to have a table with the knowledge areas of cnpq. I’m taking the Grande Areas, Areas, Sub-Areas and Specialties from a JSON file - with all the data I talked about - that I found on Github. I managed to enter the large areas and areas quietly but when it arrives in the sub-areas I have as return an error:
'Cannot add or update a child row: a foreign key constraint fails (`areas_conhecimento`.`sub_areas`, CONSTRAINT `area` FOREIGN KEY (`area`) REFERENCES `areas` (`codigo`))'
I am using Mysql with Nodejs, the Insert I used was:
INSERT INTO areas_conhecimento.sub_areas VALUES ('Álgebra', '10100008', '10101004');
Where 10100008 is the code of a record of the area table. Follow the bank code:
create database areas_conhecimento;
use areas_conhecimento;
CREATE TABLE categorias (
nome VARCHAR(30) NOT NULL,
codigo INT NOT NULL PRIMARY KEY
);
CREATE TABLE areas (
nome VARCHAR(30) NOT NULL,
categoria INT NOT NULL,
codigo INT NOT NULL PRIMARY KEY,
CONSTRAINT categoria FOREIGN KEY (categoria)
REFERENCES categorias (codigo)
);
CREATE TABLE sub_areas (
nome VARCHAR(30) NOT NULL,
codigo INT NOT NULL PRIMARY KEY,
area INT NOT NULL,
CONSTRAINT area FOREIGN KEY (area)
REFERENCES areas (codigo)
);
CREATE TABLE especialidades (
nome VARCHAR(30) NOT NULL,
codigo INT NOT NULL PRIMARY KEY,
sub_area INT NOT NULL,
CONSTRAINT sub_area FOREIGN KEY (sub_area)
REFERENCES sub_areas (codigo)
);
Where category = Large areas and the rest is the same thing.
the JSON file is available here
I wonder how I can resolve this error and not lose data from my table.
Impossible to say for sure because you did not post the INSERT command that presented an error, with all data specified. Apparently you are reporting an area code that does not exist in table areas.
– anonimo
I hadn’t noticed rsrs. I edited the question. In summary it was the following: INSERT INTO areas_knowledge.sub_areas VALUES ('Algebra', '10100008', '10101004');
– paraguassu