“如何修复Laravel中的“ ForeignKey”错误”

时间:2019-04-27 16:50:26

标签: laravel

  

PDOException::(“” SQLSTATE [42000]:语法错误或访问冲突:1064年   您的SQL语法有误;检查与您的马对应的手册   riaDB服务器版本,用于在l的delete CASCADE上的')附近使用正确的语法   ine 1“)

C:\xampp\htdocs\projek\vendor\laravel\framework\src\Illuminate\Database\Connection.php:452
  

PDO :: prepare(“ alter table pengerjaan添加约束pengerjaan_id_kateg ori_foreign外键(id_kategori)引用kategori()删除CASC   ADE”)

C:\xampp\htdocs\projek\vendor\laravel\framework\src\Illuminate\Database\Connection.php:452

public function up(){
     Schema::create('pengerjaan', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedinteger('id_kategori');
            $table->unsignedinteger('id_pelanggan');
            $table->enum('status', array('proses', 'pending', 'hasil'));
            $table->datetime('apply_date');
            $table->timestamps();

            $table->foreign('id_kategori')->refrences('id')->on('kategori')->onDelete('CASCADE');
            $table->foreign('id_pelanggan')->refrences('id')->on('pelanggan')->onDelete('CASCADE');

        });
    }

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

1 个答案:

答案 0 :(得分:0)

您有一个拼写错误,它不是refrences,而是references,因此请更改以下两行:

$table->foreign('id_kategori')->refrences('id')->on('kategori')->onDelete('CASCADE');
$table->foreign('id_pelanggan')->refrences('id')->on('pelanggan')->onDelete('CASCADE');

与此:

$table->foreign('id_kategori')->references('id')->on('kategori')->onDelete('cascade');
$table->foreign('id_pelanggan')->references('id')->on('pelanggan')->onDelete('cascade');