我真的需要帮助的人,我使用laravel的“make:auth”功能,现在我试图用框架工作生成的“用户”表创建一个外键。但它总是抛出错误 - (一般错误:1215无法添加外键约束)。它只有一个错误,我试图在“用户”表中建立表关系。 我已经被困在这里一段时间了,继承我的代码,我需要你的帮助。提前谢谢你!
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('username');
$table->integer('number');
$table->string('email')->unique();
$table->string('avatar')->default('default.jpg');
$table->string('password');
$table->string('role');
$table->rememberToken();
$table->timestamps();
});
Schema::create('post', function (Blueprint $table) {
$table->increments('id');
$table->string('photo')->default('default.jpg');
$table->text('description');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('user');
$table->timestamps();
});
}
答案 0 :(得分:1)
外国钥匙申报应该是你的错字。尝试将其更改为:
$table->foreign('user_id')->references('id')->on('users');
希望有帮助=)