Php 什么';Laravel外键有什么问题?
我正在尝试使用Laravel的迁移创建外键Php 什么';Laravel外键有什么问题?,php,mysql,laravel,Php,Mysql,Laravel,我正在尝试使用Laravel的迁移创建外键 Schema::create('articles', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsinged(); $table->string('title'); $table->longText('body');
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsinged();
$table->string('title');
$table->longText('body');
$table->timestamp('published_at');
$table->timestamps();
});
Schema::table('articles', function($table) {
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
当我执行php artisan迁移时,我遇到以下错误
[照亮\数据库\查询异常]
SQLSTATE[HY000]:一般错误:1215无法添加外键约束
(SQL:alter TABLElaravel\u articles
add constraint
文章\u user\u id\u外键(user\u id
)参考
laravel\u用户
(id
)在删除级联上)`
方法名称中有一个输入错误。它应该是unsigned()
而不是unsigned()
:
模式生成器使用链接方法,即使链接方法unsinged
不存在,也不会引发异常。尽管键入错误,articles
表仍将被创建,但是user\u id
列将是一个有符号整数,并且由于users
表中的id
列是一个无符号整数,因此将阻止创建约束。方法名称中存在键入错误。它应该是unsigned()
而不是unsigned()
:
模式生成器使用链接方法,即使链接方法unsinged
不存在,也不会引发异常。尽管输入错误,articles
表仍将被创建,但是user\u id
列将是一个有符号整数,并且由于users
表中的id
列是一个无符号整数,因此将阻止创建约束。@MarkBaker不起作用!!这是相同的迁移吗?@MarkBaker不起作用!!这是相同的迁移吗?
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('title');
$table->longText('body');
$table->timestamp('published_at');
$table->timestamps();
});