How to pass data from one table to another where one has a relationship?

0

I created a table by migration that has relationships and I have a table with the same fields (except for the incremental id) that has the data. I need to pass the data from the table that has the data to the table that has the relationships. I made a script for this but it gives foreignkey error. Do you have another way to copy? Obg.

Edit

Original table structure with data:

CREATE TABLE IF NOT EXISTS 'locais' (
  'CODIGOLOCAL' varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  'EMPRESA' int(11) NOT NULL,
  'DESCRICAO' varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  'CODIGOASCEDENTE' varchar(14) COLLATE utf8_unicode_ci DEFAULT NULL,
  'CODIGORESULT' varchar(15) COLLATE utf8_unicode_ci DEFAULT NULL,
  'DATAATIVO' date NOT NULL,
  'RESPONSAVEL' varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  'DATADESATIVADO' date DEFAULT NULL,
  'CODIGOTIPO' varchar(9) COLLATE utf8_unicode_ci DEFAULT NULL,
  'TELEFONE' varchar(14) COLLATE utf8_unicode_ci DEFAULT NULL,
  'CENTROCUSTO' int(5) DEFAULT NULL,
  'EMAIL' varchar(70) COLLATE utf8_unicode_ci DEFAULT NULL,
  'ENDERECO' varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  'COMPLEMENTO' varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  'BAIRRO' varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  'CIDADE' varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Strange table created with Migration and Relationships:

Table structure localsoriginal

CREATE TABLE IF NOT EXISTS 'localsoriginal' (
  'id' int(10) unsigned NOT NULL,
  'codigolocal' varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  'descricaolocal' varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  'idempresa' int(10) unsigned NOT NULL,
  'codigopai' int(10) unsigned NOT NULL,
  'result' varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  'dataativo' date NOT NULL,
  'datadesativado' date NOT NULL,
  'idresponsavel' int(10) unsigned NOT NULL,
  'idtipolocal' int(10) unsigned NOT NULL,
  'telefone' varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  'centrocusto' varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  'email' varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  'endereco' varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  'complemento' varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  'birro' varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  'cidade' varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  'iduf' int(10) unsigned NOT NULL,
  'created_at' timestamp NULL DEFAULT NULL,
  'updated_at' timestamp NULL DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Indexes for dumped tables

Indexes for table localsoriginal

ALTER TABLE 'localsoriginal'
  ADD PRIMARY KEY ('id'), ADD KEY 'locals_idempresa_foreign' ('idempresa'), ADD KEY 'locals_codigopai_foreign' ('codigopai'), ADD KEY 'locals_idresponsavel_foreign' ('idresponsavel'), ADD KEY 'locals_idtipolocal_foreign' ('idtipolocal'), ADD KEY 'locals_iduf_foreign' ('iduf');

AUTO_INCREMENT for dumped tables

AUTO_INCREMENT for table localsoriginal

ALTER TABLE 'localsoriginal'
  MODIFY 'id' int(10) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=8;

Constraints for dumped tables

Limiters for table localsoriginal

ALTER TABLE 'localsoriginal'
ADD CONSTRAINT 'locals_codigopai_foreign' FOREIGN KEY ('codigopai') REFERENCES 'pais' ('id'),
ADD CONSTRAINT 'locals_idempresa_foreign' FOREIGN KEY ('idempresa') REFERENCES 'empresas' ('id'),
ADD CONSTRAINT 'locals_idresponsavel_foreign' FOREIGN KEY ('idresponsavel') REFERENCES 'responsavels' ('id'),
ADD CONSTRAINT 'locals_idtipolocal_foreign' FOREIGN KEY ('idtipolocal') REFERENCES 'tipolocals' ('id'),
ADD CONSTRAINT 'locals_iduf_foreign' FOREIGN KEY ('iduf') REFERENCES 'ufs' ('id');

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Script

<?php

require("../conexao.php");

//carga local
    $sql = "SELECT CODIGOLOCAL, EMPRESA, DESCRICAO, CODIGOASCEDENTE, CODIGORESULT, DATAATIVO, RESPONSAVEL, DATADESATIVADO, CODIGOTIPO, TELEFONE, CENTROCUSTO, EMAIL, ENDERECO, COMPLEMENTO, BAIRRO, CIDADE FROM locais";
        echo $sql;
    $res = mysqli_query($con, $sql);
    while ($f = mysqli_fetch_array($res)) {
                echo 'entrei aqui';
                $codigolocal = $f['CODIGOLOCAL'];
                $idempresa = 1;
                $descricaolocal = $f['DESCRICAO'];
                $codigopai = $f['CODIGOASCEDENTE'];
                $result = $f['CODIGORESULT'];
                $dataativo = $f['DATAATIVO'];
                $datadesativado = $f['DATADESATIVADO'];
                $idresponsavel = 1;
                $idtipolocal = $f['CODIGOTIPO'];
                $telefone = $f['TELEFONE'];
                $centrocusto = $f['CENTROCUSTO'];
                $email = $f['EMAIL'];
                $endereco = $f['ENDERECO'];
                $complemento = $f['COMPLEMENTO'];
                $birro = $f['BAIRRO'];
                $cidade = $f['CIDADE'];
                $iduf = 1;
                $created_at = '2017-01-16 22:05:17';
                $updated_at = '2017-01-16 22:05:17';
                echo  $codigolocal;

                $sqluplocal="INSERT INTO localsoriginal (codigolocal, idempresa, descricaolocal, codigopai, result, dataativo,                idresponsavel, idtipolocal, telefone, centrocusto, email, endereco, complemento, birro, cidade, iduf, created_at, updated_at) VALUES ('$codigolocal', '$idempresa', '$descricao', '$codigopai', '$result', '$dataativo', '$idresponsavel', '$idtipolocal', '$telefone', '$centrocusto', '$email', '$endereco', '$complemento', '$birro', '$cidade',
 '$iduf', '$created_at', '$updated_at)";
                echo $sqluplocal;
                $resuplocal = mysqli_query($con, $sqluplocal);
    }
//Fim carga local
echo 'fim';
?>

The error you give is relationship

    
asked by anonymous 17.01.2017 / 14:25

0 answers