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

Khu*_*ram 5 mysql laravel

当我运行迁移来创建外键约束时,我在命令提示符中收到以下错误.我需要帮助来克服它,因为我在互联网上搜索了很多并尝试了各种各样的东西但不幸的是没有一个有效

在Connection.php第647行:

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)

在Connection.php第445行:

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

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

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

请帮助我.谢谢

M S*_*que 11

你只是错误的参考

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

references()而不是referances()


Dha*_*lia 8

您在迁移中遇到拼写错误

存在代码

$table->foreign('post_id')->referances('id')->on('posts')->onDelete('cascade');
Run Code Online (Sandbox Code Playgroud)

新代码

 $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
Run Code Online (Sandbox Code Playgroud)