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

nimxete2  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(305)

当我运行迁移来创建外键约束时,在命令提示符中出现以下错误。我需要帮助克服它,因为我在互联网上搜索了很多东西,尝试了各种各样的事情,但不幸的是没有一个工作
在connection.php第647行:
sqlstate[42000]:语法错误或访问冲突:1064您的sql语法有错误;检查与您的mariadb服务器版本相对应的手册,以获得在第1行(sql:alter table)使用“on delete cascade”的正确语法 category_posts 添加约束 category_posts_post_id_fo reign 外键( post_id )参考文献 posts ()删除级联)
在connection.php第445行:
sqlstate[42000]:语法错误或访问冲突:1064您的sql语法有错误;检查与您的mariadb服务器版本相对应的手册,以获得正确的语法,以便在第1行的“delete cascade”中使用
我的外键约束迁移代码如下

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');
    }
}

请帮帮我。谢谢您

1l5u6lss

1l5u6lss1#

你只是写错了参考资料 $table->foreign('post_id')->referances('id')->on('posts')->onDelete('cascade'); 使用 references() 而不是 referances()

3df52oht

3df52oht2#

你在迁移中有拼写错误吗
存在代码

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

新代码

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

相关问题