【发布时间】:2015-12-16 15:46:36
【问题描述】:
迁移我的数据库时,出现此错误。下面是我的代码,后面是我在尝试运行迁移时遇到的错误。
代码
public function up()
{
Schema::create('meals', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('category_id')->unsigned();
$table->string('title');
$table->string('body');
$table->string('meal_av');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('category_id')
->references('id')
->on('categories')
->onDelete('cascade');
});
}
错误信息
[照亮\数据库\查询异常]
SQLSTATE[HY000]:一般错误:1005 无法创建表meal.#sql-11d2_1 4(errno: 150 "外键约束为 格式不正确”)(SQL:alter
表meals添加约束饭菜_category_id_foreign外键(category_id)引用categories(id)删除 级联)
【问题讨论】:
-
是否已经创建了类别和用户?
-
它创建用户,然后是饭菜并收到该错误,然后创建停止,并在删除 (category_id) 后成功完成迁移。
-
首先请检查您引用的表是否为
InnoDB类型?如果不是,则将其更改为InnoDB,否则外键将不起作用。