Problem SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
Problem SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
我正在尝试在 Laravel 中创建外键,但是当我使用 artisan 迁移我的 table 时,我抛出了以下错误:
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,但它根本没有帮助。
Laravel 的 increments()
创建一个“自动递增 UNSIGNED INTEGER(主键)等效列。”。您的外键列 user_id
必须与其引用的列具有完全相同的类型。
在您的 CreateProductsTable
迁移中,更改
$table->integer('user_id')->nullable();
到
$table->integer('user_id')->unsigned()->nullable();
然后重试。
我正在尝试在 Laravel 中创建外键,但是当我使用 artisan 迁移我的 table 时,我抛出了以下错误:
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,但它根本没有帮助。
Laravel 的 increments()
创建一个“自动递增 UNSIGNED INTEGER(主键)等效列。”。您的外键列 user_id
必须与其引用的列具有完全相同的类型。
在您的 CreateProductsTable
迁移中,更改
$table->integer('user_id')->nullable();
到
$table->integer('user_id')->unsigned()->nullable();
然后重试。