laravel create migration with migration会引发错误或访问冲突

时间:2017-06-14 16:51:13

标签: mysql syntax-error laravel-5.4 laravel-migrations

我创建了一个触发器来检查该值是否在指定范围之间。现在我想为它进行迁移但是我收到以下错误:

  

[PDOException]     SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;检查手册      对应于您的MySQL服务器版本,以便在'delimiter |附近使用正确的语法                 DROP TRIGGER IF EXISTS header_range_limit |                 '第1行

我的迁移看起来像

class HeaderRangeTrigger extends Migration
{
/**
 * Create trigger. Checks if a value is > 100 or < 0, sets to 50 is so
 *
 * @return void
 */
public function up()
{
    DB::unprepared('
    delimiter |
        DROP TRIGGER IF EXISTS header_range_limit |
        CREATE TRIGGER header_range_limit BEFORE
        UPDATE
        ON
          user_settings FOR EACH ROW BEGIN IF NEW.header_position < 0 OR NEW.header_position > 100 THEN
        SET NEW.header_position = 50; 
          END IF; 
        END |
        delimiter ;
    ');
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    DB::unprepared('DROP TRIGGER IF EXISTS header_range_limit');
}

}

我也尝试了不同的分隔符,但结果相同。

如果我复制&amp;将代码粘贴到phpmyadmin SQL编辑器中,它没有任何问题。我错过了什么?

1 个答案:

答案 0 :(得分:0)

 public function up()
{

    $procedure ="DROP PROCEDURE IF EXISTS GetInstagramPerContest;

       CREATE PROCEDURE GetInstagramPerContest(IN  p_contest_id INT(11))
       BEGIN
                select sum(tt.instatagspercontest) from (
                select latest_tags_count as instatagspercontest from ENInstagramTagsCount where contest_id= p_contest_id
                 UNION
                select latest_tags_count as tweetspercontest from CHZHInstagramTagsCount where contest_id= p_contest_id
                 UNION
                select latest_tags_count as tweetspercontest from JPInstagramTagsCount where contest_id= p_contest_id
                 UNION
                select latest_tags_count as tweetspercontest from KRInstagramTagsCount where contest_id= p_contest_id ) as tt;
        END
        ";



    DB::connection()->getPdo()->exec($procedure);

}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    DB::unprepared("DROP PROCEDURE IF EXISTS GetInstagramPerContest");

}