laravel 5.6.33中的一般错误:1(“语法错误”)

2ul0zpep  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(244)

我对laravel迁移有些问题。我的文章和标签之间有一种多对多的关系“迁移:

Schema::create('article_tag', function (Blueprint $table) {
    $table->integer('article_id')->unsigned()->index();
    $table->foreign('article_id')->refrences('id')->on('tags')->onDelete('cascade')->onUpdate('cascade');
    $table->integer('tag_id')->unsigned()->index();
    $table->foreign('tag_id')->refrences('id')->on('articles')->onDelete('cascade')->onUpdate('cascade');
});

“标记”迁移:

Schema::create('tags', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
});

'文章迁移:

Schema::create('articles', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->text('body');
    $table->timestamps();
});

我的控制台日志:
illuminate\database\queryexception:sqlstate[hy000]:常规错误:1 near“):语法错误(sql:create table“article\u tag”(“article\u id”integer not null,“tag\u id”integer not null),外键(“article\u id”)引用“tags”()on delete cascade on update cascade,外键(“tag_id”)引用“articles”()on delete cascade on update cascade)
异常跟踪:
1 pdoexception::(“sqlstate[hy000]:常规错误:1 near”)“:语法错误”)/home/user/desktop/blog/vendor/laravel/framework/src/illuminate/database/connection。php:452
2 pdo::prepare(“create table”article\u tag“(“article\u id”integer not null,“tag\u id”integer not null,foreign key(“article\u id”)references“tags”()on delete cascade on update cascade,外键(“tag_id”)引用“articles“()on delete cascade on update cascade)”)/home/user/desktop/blog/vendor/laravel/framework/src/illuminate/database/connection。php:452
最好的,贾瓦德

cgvd09ve

cgvd09ve1#

您的迁移中有一个拼写错误。你在说什么 refrences->() 但应该是这样 references->() ```
Schema::create('article_tag', function (Blueprint $table) {
$table->integer('article_id')->unsigned()->index();
// in here
$table->foreign('article_id')->refrences('id')->on('tags')->onDelete('cascade')->onUpdate('cascade');
$table->integer('tag_id')->unsigned()->index();
// and in here too
$table->foreign('tag_id')->refrences('id')->on('articles')->onDelete('cascade')->onUpdate('cascade');
});

相关问题