【问题标题】:Laravel Migration DELIMITERLaravel 迁移DELIMITER
【发布时间】:2016-05-20 19:17:06
【问题描述】:

我正在尝试通过工匠迁移创建 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();
                });

创建“学院”表。

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

【问题讨论】:

  • 您是否生成了任何迁移文件?
  • 是的。我有几个迁移。这是唯一一个抛出错误的。
  • 您可以使用迁移代码编辑您的帖子吗?
  • 完成。编辑了我原来的帖子。

标签: mysql migration database-migration laravel-5.2 laravel-artisan


【解决方案1】:

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

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

这个问题和你的很相似。

【讨论】:

    猜你喜欢
    • 2019-12-27
    • 2018-04-18
    • 2021-02-28
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    相关资源
    最近更新 更多