SQLSTATE[23000]: Integrity Constraint Violation: 1452 Cannot add or update a Child Row Laravel

Asked

Viewed 2,910 times

0

Well guys, even though I checked several responses in the forum, I still can’t solve my problem.

Schema::create('alunos', function (Blueprint $table) {
            $table->integer('id_academia')->unsigned();
            $table->increments('id_aluno');
            $table->string('id_aluno_academia', 3);
            $table->string('foto')->nullable();
            $table->string('nome');
            $table->string('email', 70);
            $table->string('celular', 16);
            $table->string('telefone', 16)->nullable();
            $table->date('data_nasc');
            $table->string('idade', 2);
            $table->string('cpf', 14);
            $table->string('rg', 12)->nullable();
            $table->boolean('sexo');
            $table->string('cep')->nullable();
            $table->string('endereco');
            $table->string('num_end');
            $table->string('bairro');
            $table->string('cidade', 50);
            $table->string('uf', 2);
            $table->string('plano', 1)->nullable();
            $table->string('tipo_pagamento', 1)->nullable();
            $table->string('turma', 3)->nullable();
            $table->string('professor', 3)->nullable();
            $table->string('senha')->nullable();
            $table->string('status', 1)->default(0);
            $table->date('expira')->nullable();
            $table->boolean('acesso')->default(0);
            $table->string('tipo_visita', 1)->nullable();
            $table->string('indicado_por', 1)->nullable();
            $table->string('modalidades_interesse')->nullable();
            $table->string('nivel_interesse')->nullable();
            $table->string('objetivo')->nullable();
            $table->text('observacoes')->nullable();
            $table->timestamps();
            $table->foreign('id_academia')->references('id_academia')->on('academias')->onDelete('cascade');
        });

and my seeder:

public function run()
    {
        $nomes = 
        [
            'Felipe', 'João', 'Gabriela', 'Miguel', 'Renata', 'Juliana', 'Sintia', 'Marcos',
            'Débora', 'Diego', 'Carlos', 'Fabiano', 'Maria', 'Jurema', 'Joaquim', 'Artur',
            'Eduarda', 'Antonella', 'Julieta', 'Ema'
        ];

        $sobrenomes = 
        [
            'Silva', 'Santos', 'Paz', 'Guedes', 'Silveira', 'Ribeiro', 'Magalhães', 'Garcia',
            'Agostini', 'Goldmann', 'Schulz', 'Schmidt', 'Bellini'
        ];

        $ufs = 
        [
            'AC','AL','AM','AP','BA','CE','DF','ES','GO','MA','MG','MS','MT',
            'PA','PB','PE','PI','PR','RJ','RN','RO','RR','RS','SC','SE','SP','TO'
        ];

        $emails =
        [
            '@gmail.com', '@outlook.com', '@yahoo.com', '@uol.com.br', '@msn.com', '@bol.com.br'
        ];

        $alunos = [];

        for ($i=1; $i < 2; $i++) { 

            for ($c=0; $c < 14; $c++) { 
                $cpf[$c] = rand(0,9);
            }

            for ($r=0; $r < 10; $r++) { 
                $rg[$r] = rand(0,9);
            }

            for ($n=0; $n < 3; $n++) { 
                $num[$n] = rand(0,9);
            }

            $dia = rand(1, 26);
            $mes = rand(1, 12);
            $ano = rand(2012, 2017);

            $nome = $nomes[array_rand($nomes)];
            $sobrenome = $sobrenomes[array_rand($sobrenomes)];
            $email = strtolower($nome . '.' . $sobrenome).$emails[array_rand($emails)];

            $expira = $ano . '-' . $mes . '-' . $dia;
            $sexo = rand(0,1);

            $alunos =
            [
                'id_academia'         => rand(1, 10),
                'id_aluno_academia'   => $i,
                'nome'                => $nome.' '.$sobrenome,
                'email'               => $email,
                'celular'             => '(54) 9 9945-2428',
                'data_nasc'           => '1989-09-01',
                'idade'               => rand(18, 45),
                'cpf'                 => implode('',$cpf),
                'rg'                  => implode('', $rg),
                'sexo'                => $sexo,
                'endereco'            => 'Rua Maratona',
                'num_end'             => implode('', $num),
                'bairro'              => 'Vila Isabel',
                'cidade'              => 'Passo Fundo',
                'uf'                  => $ufs[array_rand($ufs)],
                'plano'               => rand(1,4),
                'senha'               => md5(time())
            ];

            DB::table('alunos')->insert($alunos);
        }
        // $alunos->save();
    }

It sounds like a joke, but yesterday it worked correctly, today, I can’t identify the mistake.

[Pdoexception]
SQLSTATE[23000]: Integrity Constraint Violation: 1452 Cannot add or update a Child Row: a Foreign key Constraint fails (gymdoctor.alunos, CONSTRAINT alunos_id
_academia_foreign
FOREIGN KEY (id_academia) REFERENCES academias (id_academia) ON DELETE CASCADE)

  • you registered in the table academia? because there needs to be data here to work properly you are making a rand(1,10) is not wrong?

  • 1

    Virgilio Novic, that’s exactly what you said. I put the Rand but this time with the Ids that contained in the database of the academies and it worked out the Seed. Thanks!!!!

No answers

Browser other questions tagged

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