【问题标题】:errno: 150 "Foreign key constraint is incorrectly formed" in Laravelerrno:150“外键约束的格式不正确”在 Laravel
【发布时间】:2019-07-14 22:09:09
【问题描述】:
Schema::create('position', function (Blueprint $table) {
        $table->increments('post_id');
        $table->String('post_name');
        $table->timestamps();
    });

Schema::create('candidate', function (Blueprint $table) {
        $table->increments('id');
        $table->String('name');
        $table->String('branch');
        $table->unsignedInteger('post_id');
        // $table->foreign('post_id_no')->references('post_id')->on('position')->onDelete('cascade');
        $table->foreign('post_id')->references('post_id')->on('position')->onDelete('cascade');
        $table->integer('count')->default(0);
        $table->timestamps();
    });

我有两张桌子,positioncandidate。当我迁移时,我收到外键错误。谁能说出代码中有什么错误?

这是我在迁移时遇到的错误:

Illuminate\Database\QueryException : SQLSTATE[HY000]: 一般错误: 1005 无法创建表voting.#sql-16b7_2b (errno: 150 "外键约束格式不正确") (SQL: alter table @987654326 @添加约束candidate_post_id_foreign外键(post_id)在删除级联时引用positionpost_id

catch (Exception $e) {
         throw new QueryException(
              $query, $this->prepareBindings($bindings), $e
          );
      }

PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `voting`.`#sql-16b7_2b` (errno: 150 "Foreign key constraint is incorrectly formed")")

【问题讨论】:

  • 在哪里是表“投票”模式,因为它在创建表投票时显示错误。

标签: laravel


【解决方案1】:

外键列可能不匹配,而引用列的类型或长度可能不同。而不是

$table->unsignedInteger('post_id');

在您的candidate 表中,尝试:

$table->integer('post_id')->unsigned()->index();

此外,将原始 (position) id 保留为“id”有时会更有帮助且更清晰(可能对 mysql 而言)。您可以在 candidate 表中将其称为 post_id,并在 position 上引用“id”。更容易理解。

【讨论】:

    【解决方案2】:

    您需要将默认引擎指定为

    $table->engine = 'InnoDB';
    

    并确保您已创建表 candidate 所引用的 position 表。请在帖子中添加您的投票表架构。

    【讨论】:

      【解决方案3】:

      你可以试试这个。

      $table->integer('post_id')->unsigned();
      $table->foreign('post_id')->references('post_id')->on('position')->onDelete('cascade');
      

      这对我有用。谢谢。

      【讨论】:

        【解决方案4】:
        public function up() {     Schema::create('companies', function (Blueprint $table) {         $table->bigIncrements('id');         $table->string('name');         $table->text('address');         $table->string('tel1');         $table->string('tel2');         $table->integer('owner');         $table->unsignedBigInteger('access_id');         $table->string('depot_number')->default(2);         $table->timestamps();           $table->foreign('access_id')->references('id')->on('accesses')             ->onDelete('cascade');     }); } 
        
        public function up() {     Schema::create('accesses', function (Blueprint $table) {         $table->bigIncrements('id');         $table->string('type');         $table->string('description');         $table->timestamps();     }); } 
        

        在您的 database/migrations 文件夹中,按名称排序。然后确保 create_accesses_tablecreate_companies_table 之前: enter image description here

        【讨论】:

          猜你喜欢
          • 2017-04-13
          • 2019-09-17
          • 2020-12-16
          • 1970-01-01
          • 1970-01-01
          • 2021-12-30
          • 2017-05-29
          • 2018-06-19
          相关资源
          最近更新 更多