Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Migration DELIMITER

I'm trying to create a MySQL trigger via artisan migration.

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
    $$
');

Nevertheless when I run the migration, I get the error

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

Help someone?

Edit 1: this are the migration files i'm using on MySQL

Create the "courses" table with the one to many relationship to faculty (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();
                });

Create the "faculties" table.

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

Create the "course_affinities" table with the many to many relationship self relationship between courses.

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

Create the "faculty_affinities" table with the many to many relationship self relationship between faculties.

    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();
                });
like image 791
Sebastian M Avatar asked Nov 09 '22 20:11

Sebastian M


1 Answers

The problem is in the DELIMITER command. This command is intended to use just in MySQL Client. So, you can't use it in Laravel.

Take a time to read this post: Creating MYSQL Procedure in Laravel 4 Migrations

It's a problem very similar to yours.

like image 104
Laerte Avatar answered Nov 15 '22 07:11

Laerte