小编Khu*_*ram的帖子

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

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

在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)

请帮助我.谢谢

mysql laravel

5
推荐指数
2
解决办法
4951
查看次数

标签 统计

laravel ×1

mysql ×1