Error while running migrate

Asked

Viewed 39 times

0

Schema::create('filiais', function (Blueprint $table) {
        $table->id();
        $table->string('filial', 30);
        $table->timestamps();
    });

    Schema::create('produto_filiais', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('filial_id');
        $table->unsignedBigInteger('produto_id');
        $table->decimal('preco_venda', 8, 2);
        $table->integer('estoque_minimo', 8, 2);
        $table->integer('estoque_maximo', 8, 2);
        $table->timestamps();

        $table->foreign('filial_id')->references('id')->on('filiais');
        $table->foreign('produto_id')->references('id')->on('produtos');
    });

    Schema::table('produtos', function (Blueprint $table) {
        $table->dropColumn(['preco_venda', 'estoque_minimo', 'estoque_maximo']);
    }); 

After rotating the php artisan migrate, he makes the following mistake:

SQLSTATE[42000]: Syntax error or access Violation: 1075 Incorrect table Definition; there can be only one auto column and it must be defined as a key (SQL: create table produto_filiais (id bigint unsigned not null auto_increment Primary key, filial_id bigint unsigned not null, produto_id bigint unsigned not null, preco_venda decimal(8, 2) not null, estoque_minimo int unsigned not null auto_increment Primary key, estoque_maximo int unsigned not null auto_increment Primary key, created_at timestamp null, updated_at timestamp null) default Character set utf8mb4 collate 'utf8mb4_unicode_ci')

  • the message is very clear "there can be only one auto column and it must be defined as a key", in free translation "there may be only one automatic column and should be set as a key"

  • If you wish to use primary key auto increment see https://laravel.com/docs/8.x/migrations#column-method-increments and https://laravel.com/docs/8.x/migrations#column-method-bigIncrements

1 answer

0


welcome to the community.

Apparently the error is occurring because the migration is trying to create more than one field with the resource auto_increments and this is not possible in Mysql.

Solution:

Remove the parameters being passed to the integer fields, they are being misinterpreted by the framework.

Anything leaves the why you passed these values so we understand better your need.

Reading the log to understand the error: Log de erro enviado pelo colaborador The error log is saying that we have 3 columns trying to be the primary key, which ends up causing the error.

That is, the problem is in the columns defined as integer that you are passing some parameters to more... Observing what can happen when creating an Integer column we can see that the framework expects to receive:

Campos esperados ao criar uma coluna integer

Where is the error

1st parameter = column name (string) 2nd parameter = autoIncrement (boolean) 3rd parameter = unsigned(boolean)

Note that as you pass

$table->integer('estoque_minimo', 8, 2);
$table->integer('estoque_maximo', 8, 2);

The framework is reading the numbers "8" as an instruction to make the field auto-increment (I don’t even know how it works, because you’re waiting for a Boolean)

Going beyond

It is possible to go deeper and understand how the integer structure is read and worked by the framework, if you get curious you can take a look at the file \vendor\laravel\framework\src\Illuminate\Database\Schema\Blueprint.php Imagem de como uma coluna integer é interpretada pelo construtor

Browser other questions tagged

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