首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel,Migrations,Foreign约束不正确

Laravel,Migrations,Foreign约束不正确
EN

Stack Overflow用户
提问于 2018-06-15 04:32:46
回答 1查看 382关注 0票数 1

我有两张桌子,即。产品和机构。在机构和产品中都有一个主键pid自动递增,一个id32长度的varchar

每个product都需要引用它的institution,因此我在products中设置institution列,并希望它是引用机构的id的外键。

然而,我一直在犯这个错误,我花了一整晚的时间试图弄清楚这个错误,但没有成功。

代码语言:javascript
复制
$ php artisan migrate
Migration table created successfully.

   Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `devbase`.`#sql-824_f23` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `institutions` add constraint `institutions_id_foreign` foreign key (`id`) references `institution` (`products`) on delete RESTRICT)

  at [...]\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `devbase`.`#sql-824_f23` (errno: 150 "Foreign key constraint is incorrectly formed")")
      [...]\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  2   PDOStatement::execute()
      [...]\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  Please use the argument -v to see more details.

这是对机构表的迁移,文件名是2018_06_14_165449_create_institutions_table.php

代码语言:javascript
复制
public function up()
{
    Schema::create('institutions', function (Blueprint $table) {
        $table->increments('pid');
        $table->string('id', 32)->unique();
        $table->string('shortname', 16)->unique();
        $table->text('fullname');
        $table->string('logo', 100);
        $table->timestamps();
    });
}

同时,这是对products表的迁移,文件名是2018_06_15_031837_create_products_table.php

代码语言:javascript
复制
public function up()
{
    Schema::enableForeignKeyConstraints();
    Schema::create('products', function (Blueprint $table) {
        $table->increments('pid');
        $table->string('id', 32)->unique();
        $table->string('url', 100)->unique();
        $table->string('shortname', 16)->unique();
        $table->string('institution', 32)->index()->nullable();
        $table->timestamps();

        $table->foreign('institution')
            ->references('institutions')
            ->on('id')
            ->onDelete('RESTRICT');
    });
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-15 04:35:40

代码语言:javascript
复制
$table->foreign('institution')
      ->references('id')
      ->on('institutions')
      ->onDelete('RESTRICT');

希望能帮上忙。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50869137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档