繁体   English   中英

无法在Laravel迁移中添加外键约束

[英]Cannot add foreign key constraint in Laravel Migrations

我目前有两个表SymbolsPrices 我目前正在为Prices创建迁移,并向Prices.symbol_id添加FK约束,该约束引用Symbols.id 这是Prices迁移:

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

Symbols.id只是$table->increments('id'); Symbols.id $table->increments('id');

但是,当我运行迁移时,会发生以下情况:

  [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

有什么想法吗?

进行新的迁移

php artisan make:migration update_prices_table

模式:

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

如果您的主键类型为bigIncrements ,请确保将bigInteger用作外键的数据类型

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM