Laravel 迁移语法错误或访问冲突 1064

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48335612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:16:15  来源:igfitidea点击:

Laravel migration syntax error or access violation 1064

mysqllaravel

提问by Khurram

When i run migrations to create foreign key constraints I get the following error in my command prompt. I need help to overcome it as i searched a lot on internet and tried variety of things but unfortunately none has worked

当我运行迁移以创建外键约束时,我在命令提示符中收到以下错误。我需要帮助来克服它,因为我在互联网上进行了大量搜索并尝试了各种方法,但不幸的是没有任何效果

In Connection.php line 647:

在 Connection.php 第 647 行中:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i n your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') on delete cascade' at line 1 ( SQL: alter table category_postsadd constraint category_posts_post_id_fo reignforeign key (post_id) references posts() on delete cascade)

SQLSTATE[42000]:语法错误或访问冲突:1064 你的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,了解在第 1 行的“删除级联”附近使用的正确语法(SQL:alter table category_postsadd constraint category_posts_post_id_fo reignforeign key ( post_id) references posts() on delete cascade)

In Connection.php line 445:

在 Connection.php 第 445 行中:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i n your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') on delete cascade' at line 1

SQLSTATE[42000]:语法错误或访问冲突:1064 你的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,了解在第 1 行的“删除级联”附近使用的正确语法

My migration code for foreign key constraint is as follows

我的外键约束迁移代码如下

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCategoryPostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('category_posts', function (Blueprint $table) {
            $table->integer('category_id')->unsigned()->index();
            $table->integer('post_id')->unsigned()->index();
            $table->foreign('post_id')->referances('id')->on('posts')->onDelete('cascade');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('category_posts');
    }
}

Kindly help me out. Thank you

请帮帮我。谢谢

回答by M Shafique

You just misspelt references

你只是拼错了参考文献

$table->foreign('post_id')->referances('id')->on('posts')->onDelete('cascade');

$table->foreign('post_id')->referances('id')->on('posts')->onDelete('cascade');

use references()instead of referances()

使用references()代替referances()

回答by Dharmesh Rakholia

You having spelling mistake in migration

你在迁移时有拼写错误

Exist code

现有代码

$table->foreign('post_id')->referances('id')->on('posts')->onDelete('cascade');

New code

新代码

 $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');