【问题标题】:Laravel migration syntax error or access violation 1064Laravel 迁移语法错误或访问冲突 1064
【发布时间】:2018-06-28 08:53:49
【问题描述】:

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

在 Connection.php 第 647 行:

SQLSTATE[42000]: 语法错误或访问冲突: 1064 You have an error i n 你的 SQL 语法;检查与您的 MariaDB 服务器相对应的手册 在第 1 行 ( SQL: alter table category_posts add constraint category_posts_post_id_fo reign foreign key (post_id) references posts () on delete cascade)

在 Connection.php 第 445 行:

SQLSTATE[42000]: 语法错误或访问冲突: 1064 You have an error i n 你的 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');
    }
}

请帮帮我。谢谢

【问题讨论】:

    标签: mysql laravel


    【解决方案1】:

    你只是拼错了references

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

    使用references() 而不是referances()

    【讨论】:

    • 完美。为我工作。
    【解决方案2】:

    您在迁移时出现拼写错误

    现有代码

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

    新代码

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-06
      • 2017-10-29
      • 1970-01-01
      • 2017-10-26
      • 2017-10-08
      • 2015-10-12
      • 2014-08-19
      • 2023-03-26
      相关资源
      最近更新 更多