Laravel迁移MySQL数据库出现“无法添加外键约束”错误
我正在开发一个Laravel应用程序。我正在使用MySQL作为数据库。我已经创建了一个模型类,并正在尝试在其上运行迁移。但是,当我运行迁移时,我在添加外键约束时出错。这就是我到目前为止所做的 首先,我迁移了运行此命令的内置Laravel用户模型Laravel迁移MySQL数据库出现“无法添加外键约束”错误,mysql,laravel,foreign-keys,laravel-migrations,Mysql,Laravel,Foreign Keys,Laravel Migrations,我正在开发一个Laravel应用程序。我正在使用MySQL作为数据库。我已经创建了一个模型类,并正在尝试在其上运行迁移。但是,当我运行迁移时,我在添加外键约束时出错。这就是我到目前为止所做的 首先,我迁移了运行此命令的内置Laravel用户模型 php artisan migrate php artisan make:model TodoItem -m php artisan migrate 用户表是在数据库中创建的 然后,我创建了另一个运行此命令的模型 php artisan migra
php artisan migrate
php artisan make:model TodoItem -m
php artisan migrate
用户表是在数据库中创建的
然后,我创建了另一个运行此命令的模型
php artisan migrate
php artisan make:model TodoItem -m
php artisan migrate
然后,我将以下代码添加到迁移文件中
class CreateTodoItemsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('todo_items', function (Blueprint $table) {
$table->text('about');
$table->integer('user_id');
$table->increments('id');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('todo_items');
}
}
正如您在上面看到的,我正在通过向todo_项目表添加外键来建立users表和todo_项目表之间的关系。然后,我尝试通过运行此命令迁移TodoItem模型
php artisan migrate
php artisan make:model TodoItem -m
php artisan migrate
当我运行命令时,我得到了这个错误
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `todo_items` add constraint `todo_items_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)
at /var/www/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: 1215 Cannot add foreign key constraint")
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458
2 PDOStatement::execute()
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458
Please use the argument -v to see more details.
我在上一个使用Postgresql的项目中也做了同样的事情。我工作得很好。对于这个项目,我正在使用MySQL数据库,现在它给了我上面的错误。这是因为您添加了$table->integer'user_id';到您的迁移文件。必须添加无符号整数而不是整数,因为users表的原始id列是无符号的,并且两列必须完全相同
[编辑]
自Laravel 5.8以来,默认用户表的id列类型不再是整数。它现在是一个大整数。更改此值
$table->integer('user_id');
对此
$table->unsignedInteger('user_id')->nullable(false);
试着像这样运行它,这应该可以 //还有,做我上面的人贴的未签名的东西
public function up()
{
Schema::create('todo_items', function (Blueprint $table) {
$table->text('about');
$table->integer('user_id');
$table->increments('id');
$table->timestamps();
});
Schema::table('todo_items', function(Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onUpdate('CASCADE')->onDelete('CASCADE');
});
}
问题是mysql在创建表时不需要外键,或者laravel以错误的顺序发出外键 简而言之,这不起作用:
Schema::create('todo_items', function (Blueprint $table) {
$table->text('about');
$table->integer('user_id')->unsigned();
$table->increments('id');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
这起到了作用:
})
Schema::table('todo_items', function(Blueprint $table){
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
试一试
请始终在代码中添加一些解释,以改进您的答案,并向未来用户提供更多信息