Welcome Guest, Not a member yet? Register   Sign In
How to perform insert in the bank with foreign key?
#1

I have two tb_Equipe and tb_Monitor tables. The tb_Equipe has relationship 1 / N with tb_Monitor , generating a foreign key in tb_Monitor .

To insert 'm using the code below :

public function Cadastrar($Mnovo)
{
$data['CGU'] = $Mnovo->getCGU();
$data['Nome'] = $Mnovo->getNome();
$data['CPF'] = $Mnovo->getCPF();
$data['Endereco'] = $Mnovo->getEndereco();
$data['Bairro'] = $Mnovo->getBairro();
$data['Cep'] = $Mnovo->getCep();
$data['Cidade'] = $Mnovo->getCidade();
$data['UF'] = $Mnovo->getUF();
$data['Complemento'] = $Mnovo->getComplemento();
$data['Senha'] = $Mnovo->getSenha();
$data['Telefone'] = $Mnovo->getTelefone();
$this->db->trans_begin();
$this->db->insert('tb_Monitor', $data);
if ($this->db->trans_status()===false) {
$this->db->trans_rollback();
return false;
}else{
$this->db->trans_commit();
return true;
}
}


But I get the following error seguite , run the code:

Error Number: 1452

Cannot add or update a child row: a foreign key constraint fails (`gbd`.`tb_monitor`, CONSTRAINT `fk_tb_Monitor_tb_Equipe` FOREIGN KEY (`idEquipe`) REFERENCES `tb_equipe` (`idEquipe`) ON DELETE NO ACTION ON UPDATE NO ACTION)

INSERT INTO `tb_Monitor` (`CGU`, `Nome`, `CPF`, `Endereco`, `Bairro`, `Cep`, `Cidade`, `UF`, `Complemento`, `Senha`, `Telefone`) VALUES ('8929999', 'Daniel', '00955674590', 'Rua Oito, 168', 'Centro', '95560-000', 'Rio de Janeiro', RJ', 'Apto ', '1234567890', '(54)8890-9089')

How do I resolve this issue foreign key ? I thank the attention .
Reply
#2

A foreign key is a constraint enforcing a relationship between two tables. In this case, it's basically saying that you can't insert a new row into tb_Monitor without a value in the field idEquipe which matches an idEquipe value in the table tb_equipe. Generally, you would resolve this by looking up the idEquipe value of the related row in tb_equipe or insert a new row in tb_equipe, get the idEquipe value for that row, then insert the row in tb_Monitor with that idEquipe value.
Reply
#3

(09-02-2015, 07:03 AM)mwhitney Wrote: A foreign key is a constraint enforcing a relationship between two tables. In this case, it's basically saying that you can't insert a new row into tb_Monitor without a value in the field idEquipe which matches an idEquipe value in the table tb_equipe. Generally, you would resolve this by looking up the idEquipe value of the related row in tb_equipe or insert a new row in tb_equipe, get the idEquipe value for that row, then insert the row in tb_Monitor with that idEquipe value.

I made an inner join when running the code returns me now that idEquipe value can not be null .

I can not point to the tb_Equipe to receive the value of the foreign key.
Reply
#4

The foreign key constraint exists to ensure that your idEquipe value is a valid reference to a value in another table, which would allow you to read the data from both tables with a join. When you're inserting data into tables with these types of relationships, though, you inevitably have to insert into them in the proper order, or retrieve data from one table to use in the insert into the other table. You don't usually use a join on the insert or update statements (and off the top of my head I'm not sure whether join is supported in insert/update statements).

If the constraints are valid, you have no choice but to get the required data or insert the rows in the proper order. If the constraints are not valid, you're going to have to change the constraints in the database.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB