我想为公司创建类别,但出现此错误:
SQLSTATE [HY000]:常规错误:1005无法创建表sanat
。#sql-53e_e7d
(错误号:150“外键约束格式不正确”)(SQL:更改表company_category
添加约束company_category_company_id_foreign
外键(company_id
)引用删除级联上的companies
(id
)
这是我的类别迁移代码:
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('name');
$table->string('slug');
$table->string('description')->nullable();
$table->string('type');
$table->string('imageUrl')->nullable();
$table->timestamps();
});
Schema::create('article_category', function (Blueprint $table) {
$table->integer('article_id')->unsigned();
$table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->primary(['article_id', 'category_id']);
});
Schema::create('company_category', function (Blueprint $table) {
$table->integer('company_id')->unsigned();
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->primary(['company_id', 'category_id']);
});
}
因此不会创建公司表和类别表。 当我删除此内容时:
Schema::create('company_category', function (Blueprint $table) {
$table->integer('company_id')->unsigned();
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->primary(['company_id', 'category_id']);
});
php artisan migration工作正常。 问题出在哪里
这是我的公司迁移:
Schema::create('companies', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->boolean('active')->default(false);
$table->string('name');
$table->string('email')->unique();
$table->string('slug');
$table->text('aboutUs');
$table->integer('tell');
$table->string('address');
$table->string('tag');
$table->string('logoUrl');
$table->string('slideUrl')->unique();
$table->string('facebookUrl');
$table->string('instagramUrl');
$table->string('telegramUrl');
$table->string('whatsAppUrl');
$table->string('websiteUrl');
$table->timestamps();
});