我在 XAMPP 堆栈上使用 Laravel 5.8。
考虑这两个迁移来创建两个表:
post_categories 表。
Schema::create('post_categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('slug');
$table->string('status');
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
帖子表。
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('slug');
$table->mediumText('description');
$table->integer('views');
$table->integer('post_category_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->timestamps();
$table->foreign('post_category_id')->references('id')->on('post_categories');
});
Run Code Online (Sandbox Code Playgroud)
当我运行时php artisan migrate,我收到以下错误:
一般错误:1005 无法创建表
testdb。#sql-38d8_102(错误号:150“外键约束的格式不正确”)。
我试过同时运行:
php artisan migratephp artisan migrate:fresh我还尝试将post_category_id字段更改为常规整数,而不是无符号:没有区别。
还重新启动了 MySQL 服务器和 Apache 服务,没有任何区别。
在post_categories迁移运行前的posts迁移。该posts表被创建,但外键不。
为什么会抛出这个错误,我该如何解决?
Jos*_*tto 11
这可能是因为您试图创建一个带有整数字段到大整数字段的外键。在你的帖子迁移中,而不是这个
$table->integer('post_category_id')->unsigned();
Run Code Online (Sandbox Code Playgroud)
做这个:
$table->bigInteger('post_category_id')->unsigned();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5226 次 |
| 最近记录: |