Laravel Migration DELIMITER

时间:2016-02-10 12:21:42

标签: mysql migration database-migration laravel-5.2 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)的一对多关系。

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”表。

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

创建“course_affinities”表,其中包含课程之间的多对多关系自我关系。

    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”表格,其中包含多个院系之间的自我关系。

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

1 个答案:

答案 0 :(得分:1)

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

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

这是一个与你非常相似的问题。