【问题标题】:Laravel, Migrations, Foreign Key constraint is incorrectly formedLaravel,迁移,外键约束形成不正确
【发布时间】:2018-06-15 04:32:46
【问题描述】:

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

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

但是,我不断收到此错误,我花了整整一夜试图解决这个问题,但没有任何成功。

$ 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

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();
    });
}

虽然这是产品表的迁移,但文件名是2018_06_15_031837_create_products_table.php

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');
    });
}

【问题讨论】:

    标签: php laravel foreign-keys migration


    【解决方案1】:
    $table->foreign('institution')
          ->references('id')
          ->on('institutions')
          ->onDelete('RESTRICT');
    

    希望对你有帮助。

    【讨论】:

    • 这是我的 stupid。我不应该得到高和代码。感谢您的宝贵时间。
    猜你喜欢
    • 2022-11-27
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 2019-07-27
    • 2018-05-23
    • 2015-12-16
    • 2019-09-10
    • 2020-11-12
    相关资源
    最近更新 更多