Error with.mysql.jdbc.exceptions.jdbc4.Mysqlintegrityconstraintviolationexception: Cannot add or update a Child Row: a Foreign key Constraint fails

Asked

Viewed 142 times

0

Good people, I have this mistake and I can’t see the solution. I’m working on my first post-college project and I’m in need of some guidance.

I have tables: Client, Ordemdeservico, Budget, Service and Budget.

The Customer, Service Order, Budget and Sevico tables perform all operations as scheduled. The error happens when I try to insert services in the table Orcamentotemservico.

Below I will put an example of the test I did and the error that occurs:

public OrcamentoTemServicoDaoTest() {
}

@Test
public void insetir() {
    Cliente c = new Cliente();
    c.setCPF("79167357504");
    OrdemDeServico ods = new OrdemDeServico();
    ods.setNumeroOS(874);
    Orcamento o = new Orcamento();
    o.setID(1);
    Servico s = new Servico();
    s.setID(1);
    s.setServico("teste");
    s.setPrecoVenda(100);
    OrcamentoTemServico ots = new OrcamentoTemServico();
    ots.setOrcamentoID(o);
    ots.setOrdemDeServicoNumeroOS(ods);
    ots.setOrdemDeServicoClienteCPF(c);
    ots.setQuantidade(1);
    ots.setServicoID(s);
    ots.setValor(100);
    ots.setValorTotal(100);
    OrcamentoTemServicoDao otsdao = new OrcamentoTemServicoDao();
    otsdao.insert(ots);
}

It says that the test was approved however it presents the error and no information is saved in my table Orcamentotemservico:

Error with.mysql.jdbc.exceptions.jdbc4.Mysqlintegrityconstraintviolationexception: Cannot add or update a Child Row: a Foreign key Constraint fails (gtsassist.ORCAMENTO_has_SERVICO, CONSTRAINT fk_ORCAMENTO_has_SERVICO_ORCAMENTO1 FOREIGN KEY (ORCAMENTO_ID, ORCAMENTO_ORDEM_DE_SERVICO_NUMERO_OS, `ORCAMENTO_ORDEM_DE_SERVICO_CLIENTE_C)

From now on, thank you very much for the return of the solution.

  • Did you relate the foreign keys of the table that receives the data to the tables that are in the database ? "the Foreign key Constraint fails ".

  • It would be nice to edit the question because Java code is kind of irrelevant, it’s an SQL problem.

2 answers

0

When I made a method to delete User from my database, I returned this error, and the reason for the error was that the User I was trying to delete, was with Requests linked to it, and if I deleted, I would also delete Orders, so, I added a new User to test, deleted it, and it worked normally. Try to see if what you are inserting or deleting is linked to other classes.

  • 1

    Your answer made me understand the mistake. I can’t add or update a daughter line, IE, the values of my FK should be captured from the resultset and I was setting direct.

0

Follows the solution.

@Test
public void insert() {
    ClienteDao cdao = new ClienteDao();
    cdao.executeSql("SELECT CLIENTE.*, ORDEM_DE_SERVICO.*, ORCAMENTO.*, SERVICO.ID AS SERVICO_ID FROM CLIENTE, ORDEM_DE_SERVICO, ORCAMENTO, SERVICO WHERE ORDEM_DE_SERVICO.NUMERO_OS LIKE '874' GROUP BY NUMERO_OS");
    try {
        if (cdao.rs.next()) {

            Cliente c = new Cliente();
            c.setCPF(cdao.rs.getString("CPF"));

            OrdemDeServico ods = new OrdemDeServico();
            ods.setNumeroOS((Integer.valueOf(cdao.rs.getString("NUMERO_OS"))));

            Orcamento o = new Orcamento();
            o.setID(Integer.valueOf(cdao.rs.getString("ID")));
            o.setOrdemDeServicoClienteCpf(c);
            o.setOrdemDeServicoNumeroOS(ods);

            Servico s = new Servico();
            s.setID(Integer.valueOf(cdao.rs.getString("SERVICO_ID")));

            OrcamentoTemServico ots = new OrcamentoTemServico();
            ots.setOrcamentoID(o);
            ots.setNumeroOS(ods);
            ots.setCPF(c);
            ots.setServico(s);
            ots.setQuantidade(1);
            ots.setValor(100);
            ots.setValorTotal(100);

            OrcamentoTemServicoDao otsdao = new OrcamentoTemServicoDao();
            otsdao.insert(ots);
        }
    } catch (SQLException ex) {
        Logger.getLogger(OrcamentoTemServicoDaoTest.class.getName()).log(Level.SEVERE, null, ex);
    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.