Error while running Migration

Asked

Viewed 58 times

1

Error:

1005 Can’t create table 'cobranca_simples. #sql-f04_1f0' (Errno: 150) (SQL: alter table carteira_cliente add Constraint carteira_cliente_carteira_id_foreign Foreign key (carteira_id) References carteiras (id) on delete)

Migrations:

Schema::create('carteiras', function (Blueprint $table) {
        $table->integer('id');
        $table->string('descricao');
        $table->timestamps();
    }
);

Schema::create('carteira_cliente', function (Blueprint $table) {
        $table->integer('carteira_id')->unsigned();
        $table->integer('cliente_id')->unsigned();

        $table->foreign('carteira_id')->references('id')
                        ->on('carteiras')->onDelete('cascade');
        $table->foreign('cliente_id')->references('id')
                        ->on('clientes')->onDelete('cascade');
        $table->primary(['carteira_id', 'cliente_id']);
    }
);
Schema::create('carteira_user', function (Blueprint $table) {
        $table->integer('carteira_id')->unsigned();
        $table->integer('user_id')->unsigned();

        $table->foreign('carteira_id')->references('id')
                        ->on('carteiras')->onDelete('cascade');
        $table->foreign('user_id')->references('id')
                        ->on('users')->onDelete('cascade');
        $table->primary(['carteira_id', 'user_id']);
    }
);
  • And where is the Migration of the table cobranca_simples?

  • simple charge is the bank name

  • It must be the order of creation, if you did wrong you have to change the name of the file. It’s an assumption only, only this has no way of knowing, is a local error difficult to reproduce.

1 answer

2

Friend, very simple to solve.

carteiras.id tem sinal 
carteira_cliente.carteira_id não tem sinal (unsigned)

Both must be equal.

Responding to the comment of the friend Thalles Rangel, in more detail.

According to the official Mysql documentation, the corresponding columns must be of the same type. For integers, the sign and length must be equal. For strings, the length need not be equal. For non-binary strings, the charset and collation must be equal.

Corresponding Columns in the Foreign key and the referenced key must have similar data types. The size and Sign of integer types must be the same. The length of string types need not be the same. For nonbinary (Character) string Columns, the Character set and collation must be the same.

In practical terms, imagine a bank with the tables "city" and "country".

address

So if the table/column country country_id is an UNSIGNED SMALLINT, so the table/column city country_id should also be an unsigned smallint.

Signs in numeric fields indicate whether or not the number will have a negative value.

Look at the Mysql integer table:

The SMALLINT of my example, with signal has a range of -32768 ~ 32767. Already no signal (UNSIGNED) has a range of 0 ~ 65535.

So there at the Migration, wallets.id has a sign (negative value), and portfolia_client.porty_id no sign (only positive value). Therefore it has different signals and gives rise to error.

The right thing would be:

Schema::create('carteiras', function (Blueprint $table) {
        $table->unsignedInteger('id');
        //ou se for chave primária com auto incremento 
        //$table->increments('id'); 
        $table->string('descricao');
        $table->timestamps();
    }
);

I hope I made it clear.

Hugs.

  • could clarify better, did not understand your suggestion..

Browser other questions tagged

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