dslh32311 2017-01-10 15:26
浏览 98
已采纳

在Laravel 4迁移中创建MYSQL功能

I am working on laravel 4.1. I want to create MYSQL function with migration but it doesn't work with php artisan migrate. even if it shows Migrated: 2017_01_10_140002_create_MYSQL_UNAVAILABLE_PRODS_FUNCTION in the console. and when i copy/past the same creation code in MYQL client, it works and the function is created. and when i run php artisan migrate:rollback the function is removed.
Any help would be appreciated.

the migration file :

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateMYSQLUNAVAILABLEPRODSFUNCTION extends Migration
{

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $available = Constant::PRODUCT_AVAILABLE_TRUE | Constant::PRODUCT_AVAILABLE_STOCK_TRUE;

        $sql = 'DROP FUNCTION IF EXISTS UNAVAILABLE_PRODS;
                DELIMITER $$
                CREATE FUNCTION UNAVAILABLE_PRODS(CONFIG_ID INT, PROD_TYPE INT)
                  RETURNS TEXT
                  LANGUAGE SQL
                BEGIN
                    DECLARE RES TEXT;

                    SET GROUP_CONCAT_MAX_LEN = 2048;

                    SET RES = (SELECT GROUP_CONCAT(ID SEPARATOR \'|\') FROM PRODUCT_BASE WHERE `PRODUCT_TYPE` & PROD_TYPE AND `PRODUCT_BASE`.`AVAILABLE` <> ' . $available . ');

                    RETURN CONCAT(CONFIG_ID, \'=(\', RES, \')\');
                END;
                $$
                DELIMITER ;
                ';

        DB::unprepared($sql);
    }

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

}
  • 写回答

2条回答 默认 最新

  • dpjs2005 2017-01-12 10:36
    关注

    I figured out what the problem was, it's the delimiters. it worked after i removed the lignes DELIMITER $$ , $$ and DELIMITER ;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥25 activeMq在同服务器centos8下消费很慢
  • ¥15 c++多文件编译报错:undefined reference to
  • ¥20 为什么zynq CAN IP 无法进入config配置模式,XCan_SelfTest函数失效?
  • ¥15 Pycharm中程序直接运行可以但进入调试报错
  • ¥15 MATLAB动图问题
  • ¥15 有段代码不知道怎么理解,const isToken = (config.headers || {}).isToken === false
  • ¥15 我的显卡支持CUDA最高版本是12.3,这个版本也支持VS 2022 17.0这种情况下如果我想下载CUDA11.8,需要下载旧版本的VS2022吗
  • ¥15 pointnet2包安装
  • ¥20 射频功率问题,解答者有酬谢!
  • ¥80 构建降雨和积水的预测模型