我有此错误:
PDOException::(“ SQLSTATE [HY000]:常规错误:1005无法创建表
shreemad
。posts
(错误号:150”外键约束格式不正确“)”)>
表1:
public function up()
{
Schema::create('category', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('type_id');
$table->string('product_type');
$table->timestamps();
});
}
表格:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('type');
$table->foreign('type')->references('type_id')->on('category');
$table->string('title');
$table->string('description');
$table->integer('price')->nullable();
$table->mediumtext('image')->nullable();
$table->timestamps();
});
}
答案 0 :(得分:0)
您必须在外键字段上包含onDelete和on Update方法。
$table->unsignedBigInteger('type');
$table->foreign('type')->references('type_id')->on('category')->onDelete('cascade')->onUpdate('cascade');
如果不这样做,它还会检查您的迁移顺序。类别迁移应该在帖子迁移之前。