Home > Enterprise >  Migrations fail but seems to be good in PHP LARAVEL
Migrations fail but seems to be good in PHP LARAVEL

Time:01-25

I get back:

SQLSTATE[HY000]: General error: 3780 Referencing column 'Categoria_id' and referenced column 'id' in foreign key constraint 'prodotti_categoria_id_foreign' are incompatible. (SQL: alter table prodotti add constraint prodotti_categoria_id_foreign foreign key (Categoria_id) references categorie (id))

but in my migrations data is:

// Categorie
 public function up()
    {
        //
        Schema::create('categorie', function (Blueprint $table) {
            $table->unsignedBigInteger('id')->autoIncrement();
            $table->string('Nome');
            $table->enum('Status',['Active','Disabled']);

        });
    }
// Prodotti:
public function up()
    {
        //
        Schema::create('prodotti', function (Blueprint $table) {
            $table->id();
            $table->string('Marca');
            $table->string('Nome');
            $table->string('Descrizione');
            $table->bigInteger('EAN');
            $table->bigInteger('MINSAN');
            $table->timestamps();
            $table->unsignedInteger('Categoria_id');
            $table->foreign('Categoria_id')->references('id')->on('categorie');
        });
    }

CodePudding user response:

Change the type of categoria_id as bellow;

$table->unsignedBigInteger('Categoria_id');

So it matches the foreign key.

CodePudding user response:

In categoria, you have the following:

$table->unsignedBigInteger('id')->autoIncrement();

In prodotti you have this:

$table->unsignedInteger('Categoria_id');

Notice how the type are different, one is unsignedBigInteger and the other is unsignedInteger. You'll have to use the same type.

Edit:

Why is this an issue?

Because unsignedBigInteger can grow more than unsignedInteger therefore you'll be at a point where the mapping will no longer be supported as one will continue to grow, but the other will have hit it's limit.

  •  Tags:  
  • Related