Fórum Ajuda com relacionamento em laravel #609186
21/04/2020
0
___________________________________________________________________________________
Segue erro abaixo:
Migrating: 2020_04_21_145544_cria_tabela_telefones
Illuminate\Database\QueryException
SQLSTATE[42S01]: Base table or view already exists: 1050 Table "telefones" already exists (SQL: create table `telefones` (`id` bigint unsigned not null auto_increment primary key, `cliente_id` int unsigned not null, `telefone` varchar(255) not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate "utf8mb4_unicode_ci")
at D:\Projetos\PHP\appTeste\vendor\laravel\framework\src\Illuminate\Database\Connection.php:671
667| // If an exception occurs when attempting to run a query, we"ll format the error
668| // message to include the bindings with SQL, which will make this exception a
669| // lot more helpful to the developer instead of just the database"s errors.
670| catch (Exception $e) {
> 671| throw new QueryException(
672| $query, $this->prepareBindings($bindings), $e
673| );
674| }
675|
1 D:\Projetos\PHP\appTeste\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464
PDOException::("SQLSTATE[42S01]: Base table or view already exists: 1050 Table "telefones" already exists")
2 D:\Projetos\PHP\appTeste\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464
PDOStatement::execute()
___________________________________________________________________________________
O código que estou tentando executar o relacionamento é o código abaixo:
___________________________________________________________________________________
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CriaTabelaTelefones extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create("telefones", function (Blueprint $table) {
$table->id();
$table->integer("cliente_id")->unsigned();
$table->string("telefone");
$table->timestamps();
});
//Relacionamento com a Tabela Clientes;
Schema::table("telefones", function (Blueprint $table) {
$table->foreign("cliente_id")->references("id")->on("clientes");
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists("telefones");
}
}
___________________________________________________________________________________
Podem me ajudar?
Ulisses Mesquita
Curtir tópico
+ 0Posts
22/04/2020
Ulisses Mesquita
Para eu resolver, precisei alterar a tabela cliente, especificando o campo $table->incremets('id'); para: $table->integer()->increments()->primary();
Após adicionar o atributo primary, declarando o id do cliente como chave primária, tudo funcionou como deveria funcionar!
Agradeço o auxílio!
Gostei + 0
24/04/2020
Ayrton Pereira
Que bom que conseguiu! Respondendo aqui para seu post sair dos "não respondidos", até algum ADM finalizar.
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)