【问题标题】:Problem SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint问题 SQLSTATE[HY000]: 一般错误:1215 无法添加外键约束
【发布时间】:2020-11-12 23:05:00
【问题描述】:

我正在尝试在 Laravel 中创建外键,但是当我使用 artisan 迁移我的表时,我抛出了以下错误:

  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `products` add constraint `products_user_id_foreign` foreign key (`user_id`) references `users` (`id`))

这是我的用户迁移

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('surname')->nullable();
            $table->string('showname')->nullable();
            $table->string('business')->nullable();
            $table->string('NIP')->nullable();
            $table->string('PESEL')->nullable();
            $table->string('address')->nullable();
            $table->string('city')->nullable();
            $table->string('postalcode')->nullable();
            $table->string('phone')->nullable();
            $table->string('comments')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

这是我的产品迁移

<?php


use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;


class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->text('detail')->nullable();
            $table->integer('user_id')->nullable();
            $table->string('category')->nullable();
            $table->date('launchdate')->nullable();
            $table->date('expirationdate')->nullable();
            $table->integer('billingperiod')->nullable();
            $table->integer('renewalprice')->nullable();
            $table->integer('internalcost')->nullable();
            $table->string('status')->nullable();
            $table->timestamps();
        });
        
        Schema::table('products', function (Blueprint $table){
            $table->foreign('user_id')
                  ->references('id')
                  ->on('users');
        });
        
        
        
        
        
        
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }
}

请帮助我。我不知道该怎么办。我在databaase.php中将数据库引擎更改为Inno DB,但它根本没有帮助。

【问题讨论】:

  • 你不需要这部分Schema::table('products'...你也可以在`Schema::create('products'...`函数中附加外键。
  • 基于异常,听起来数据违反了外键约束,因此无法创建。如果您在 Schema::create 中而不是在创建表之后创建约束,它是否有效?

标签: php mysql laravel frameworks


【解决方案1】:

Laravel 的 increments() 创建了一个“自动递增 UNSIGNED INTEGER(主键)等效列。”。您的外键列 user_id 必须与它所引用的列的类型完全相同。

在您的 CreateProductsTable 迁移中,更改

$table->integer('user_id')->nullable();

$table->integer('user_id')->unsigned()->nullable();

然后再试一次。

https://laravel.com/docs/7.x/migrations#creating-columns

【讨论】:

  • 或者$table-&gt;unsignedIntiger('user_id')-&gt;nullable(),如果你喜欢更少的链接。
猜你喜欢
  • 2021-03-31
  • 2018-07-29
  • 2020-03-25
  • 1970-01-01
  • 2019-08-02
  • 2021-07-03
  • 2020-01-14
  • 2022-11-18
  • 2016-12-20
相关资源
最近更新 更多