Laravel 迁移DELIMITER

Laravel Migration DELIMITER

我正在尝试通过 artisan 迁移创建 MySQL 触发器。

DB::unprepared('
    DELIMITER $$
    CREATE TRIGGER cascade_courseAffinity_after_facultyAffinity
    AFTER DELETE ON faculty_affinities
    FOR EACH ROW
    BEGIN
        DELETE ca
        FROM course_affinities AS ca
        JOIN courses AS course1 ON ca.course1_id = course1.id 
        JOIN courses AS course2 ON ca.course2_id = course2.id    
        WHERE (course1.faculty_id = OLD.faculty1_id OR course1.faculty_id = OLD.faculty2_id)
        AND (course2.faculty_id = OLD.faculty1_id OR course2.faculty_id = OLD.faculty2_id);
        END
    $$
');

然而,当我 运行 迁移时,我得到了错误

SQLSTATE[]: Syntax error... near DELIMITER $$ at line 1

帮助别人?

编辑 1:这是我在 MySQL

上使用的迁移文件

创建与教师 (faculty_id) 具有一对多关系的 "courses" table。

Schema::create('courses', function (Blueprint $table) {
                    $table->increments('id');
                    $table->string('name', 150);
                    $table->integer('faculty_id')->unsigned();
                    $table->foreign('faculty_id')->references('id')->on('faculties');
                    $table->boolean('active')->default(1);
                    //$table->softDeletes();
                });

创建 "faculties" table。

        Schema::create('faculties', function (Blueprint $table) {
                        $table->increments('id');
                        $table->string('name', 150)->unique();
                        $table->boolean('active')->default(1);
                        //$table->softDeletes();
                });

创建 "course_affinities" table 与课程之间的多对多关系自身关系。

    Schema::create('course_affinities', function (Blueprint $table) {
                    $table->increments('id');
                    $table->integer('course1_id')->unsigned();
                        $table->foreign('course1_id')->references('id')->on('courses');
                    $table->integer('course2_id')->unsigned();
                        $table->foreign('course2_id')->references('id')->on('courses');
                    $table->boolean('active')->default(1);
                    //$table->softDeletes();
                });

创建 "faculty_affinities" table 与院系之间的多对多关系 self 关系。

    Schema::create('faculty_affinities', function (Blueprint $table) {
                    $table->increments('id');
                    $table->integer('faculty1_id')->unsigned();
                        $table->foreign('faculty1_id')->references('id')->on('faculties');
                    $table->integer('faculty2_id')->unsigned();
                        $table->foreign('faculty2_id')->references('id')->on('faculties');
                    $table->boolean('active')->default(1);
                    //$table->softDeletes();
                });

问题出在 DELIMITER 命令中。此命令旨在仅在 MySQL 客户端中使用。所以,你不能在 Laravel.

中使用它

花点时间阅读这篇文章 post:Creating MYSQL Procedure in Laravel 4 Migrations

这个问题和你的很相似