1

I currently have two tables Symbols and Prices. I am currently creating a migration for Prices and adding a FK constraint to Prices.symbol_id which references Symbols.id. This is the Prices migration:

        $table->increments('id');
        ...
        ...
        $table->integer('symbol_id');
        $table->foreign('symbol_id')
            ->references('id')->on('Symbols')
            ->onDelete('cascade’);

Symbols.id is simply a $table->increments(‘id’);

However, when I run the migration, this is what happens:

  [Illuminate\Database\QueryException]                                                 
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter   
  table `Prices` add constraint prices_symbol_id_foreign foreign key (`symbol_id`) re  
  ferences `Symbols` (`id`))                  

  [PDOException]                                                          
      SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

Any ideas why?

theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169
  • Possible duplicate of [Migration: Cannot add foreign key constraint in laravel](http://stackoverflow.com/questions/22615926/migration-cannot-add-foreign-key-constraint-in-laravel) – r3mainer Mar 06 '16 at 23:33

2 Answers2

1

make a new migration

php artisan make:migration update_prices_table

The schema:

 public function up()
 {
    Schema::table('Prices', function (Blueprint $table) {
        $table->integer('symbol_id)->unsigned();
        $table->foreign('symbol_id')
        ->references('id')->on('Symbols')
        ->onDelete('cascade’);
    });
}
Med
  • 2,772
  • 1
  • 11
  • 14
1

If your primary key is of type bigIncrements , make sure you use bigInteger as datatype for the foreign Key

usb-bush
  • 11
  • 1