我想为公司创建类别,但出现以下错误:sqlstate[hy000]:常规错误:1005无法创建表 sanat
. #sql-53e_e7d
(errno:150“外键约束格式不正确”)(sql:alter table 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']);
});
}
所以company表和categories表不创建。当我删除此项时:
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迁移成功。问题出在哪里
这是我的公司迁移:
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();
});
暂无答案!
目前还没有任何答案,快来回答吧!