【发布时间】:2019-11-27 20:30:25
【问题描述】:
我不明白我的迁移设置有什么问题。当我尝试为已存在的表添加外键时出现此错误。
Migrating: 2019_11_27_201351_add_foreign_key_to_tags
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table `tip`.`tags` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `tags` add constraint `tags_website_id_foreign` foreign key (`website_id`) references `websites` (`id`))
我尝试在其上创建外键的表
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('tagname')->nullable();
$table->integer('website_id')->unsigned();
$table->string('tag_category');
$table->timestamps();
});
}
这是我尝试参考的表格:
public function up()
{
Schema::create('websites', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('websiteName')->nullable();
$table->timestamps();
});
}
这就是我所说的迁移
public function up()
{
Schema::table('tags', function (Blueprint $table) {
//
$table->foreign('website_id')->references('id')->on('websites');
});
}
在创建所有表后调用此迁移。任何帮助表示赞赏
【问题讨论】:
-
在这里发现了类似的问题:stackoverflow.com/questions/32669880/…
标签: php laravel foreign-keys migration relational-database