Laravel MariaDB errno:150 "Foreign key constraint is incorrectly formed"
Laravel MariaDB errno:150 "Foreign key constraint is incorrectly formed"
我的帖子迁移是:
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('channel_id');
$table->foreign('channel_id')->references('id')->on('channels');
$table->string('title');
$table->text('content');
$table->string('status')->default('published');
$table->string('type');
$table->string('published_at');
$table->timestamps();
});
和频道:
Schema::create('channels', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->string('channel_id');
$table->timestamps();
});
现在,当我执行 php artisan migrate
时出现以下错误消息:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table `TBL_NAME`.`#sql-
2221_1f76c` (errno: 150 "Foreign key constraint is incorrectly formed") (SQ
L: alter table `posts` add constraint `posts_channel_id_foreign` foreign ke
y (`channel_id`) references `channels` (`id`))
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table `TBL_NAME`.`#sql-
2221_1f76c` (errno: 150 "Foreign key constraint is incorrectly formed")
我正在使用 Laravel 5.3,并且迁移在我使用 MySQL 的本地计算机上完美运行。但是当我在服务器上上传我的 Laravel 项目并在服务器上使用 MariaDB 时。我看到了那个错误信息。
在 posts
table 迁移中更改此:
$table->string('channel_id');
为此:
$table->integer('channel_id')->unsigned();
此外,请确保在 posts
table 迁移之前 运行 channels
table 迁移。
我的帖子迁移是:
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('channel_id');
$table->foreign('channel_id')->references('id')->on('channels');
$table->string('title');
$table->text('content');
$table->string('status')->default('published');
$table->string('type');
$table->string('published_at');
$table->timestamps();
});
和频道:
Schema::create('channels', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->string('channel_id');
$table->timestamps();
});
现在,当我执行 php artisan migrate
时出现以下错误消息:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table `TBL_NAME`.`#sql-
2221_1f76c` (errno: 150 "Foreign key constraint is incorrectly formed") (SQ
L: alter table `posts` add constraint `posts_channel_id_foreign` foreign ke
y (`channel_id`) references `channels` (`id`))
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table `TBL_NAME`.`#sql-
2221_1f76c` (errno: 150 "Foreign key constraint is incorrectly formed")
我正在使用 Laravel 5.3,并且迁移在我使用 MySQL 的本地计算机上完美运行。但是当我在服务器上上传我的 Laravel 项目并在服务器上使用 MariaDB 时。我看到了那个错误信息。
在 posts
table 迁移中更改此:
$table->string('channel_id');
为此:
$table->integer('channel_id')->unsigned();
此外,请确保在 posts
table 迁移之前 运行 channels
table 迁移。