2016-02-10 186 views
2

我想通過工匠遷移創建一個MySQL觸發器。Laravel遷移DELIMITER

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

創建「學院」表。

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

您是否生成了任何遷移文件? – Laerte

+0

是的。我有幾個遷移。這是唯一一個拋出錯誤的人。 –

+0

你可以用遷移代碼編輯你的文章嗎? – Laerte

回答