Laravel 5.6.33中的常规错误:1接近")":语法错误")

Jav*_*vad 1 php mysql sqlite laravel

我对Laravel Migrations有一些问题.我的文章和标签之间有很多关系.'article_tag'迁移:

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');
});
Run Code Online (Sandbox Code Playgroud)

'tags'迁移:

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

'文章迁移:

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

我的控制台日志:

Illuminate\Database\QueryException:SQLSTATE [HY000]:一般错误:1靠近")":语法错误(SQL:create table"article_tag"("article_id"integer not null,"tag_id"integer not null,外键("article_id" ")在更新级联上的删除级联上引用"tags"(),外键("tag_id")引用更新级联上的删除级联上的"articles"()

异常跟踪:

1 PDOException ::("SQLSTATE [HY000]:一般错误:1近")":语法错误")/home/user/Desktop/blog/vendor/laravel/framework/src/Illuminate/Database/Connection.php:452

2 PDO :: prepare("create table"article_tag"("article_id"integer not null,"tag_id"integer not null,外键("article_id")引用"标记"()关于更新级联上的删除级联,外键( "tag_id")在更新级联上引用"删除级联"上的"文章"())")/ home/user/Desktop/blog/vendor/laravel/framework/src/Illuminate/Database/Connection.php:452

最好的,贾瓦德

Teo*_*gır 5

您的迁移中存在拼写错误.你说的是,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');
    });
Run Code Online (Sandbox Code Playgroud)

  • 别后悔,每个人都会犯这些错误。 (3认同)