与PostgreSQL一起使用时EF核心迁移脚本错误

时间:2019-03-15 04:52:31

标签: postgresql ef-core-2.2

我有以下迁移脚本。我不知道如果只运行创建或替换功能阻止它起作用,为什么会出错。

DO $$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20190305152943_SupportLocationAndAsset') THEN

    CREATE OR REPLACE FUNCTION build_hierarchey(location_id int default null::int, prefer_depth int default null::int) 
    RETURNS SETOF jsonb AS 
    $$
    BEGIN
        IF prefer_depth < 0 THEN
            RETURN;
        ELSE 
            prefer_depth := prefer_depth - 1;
            RETURN QUERY 
            SELECT
                CASE WHEN COUNT(x) > 0 
                    THEN (((to_jsonb(t) || jsonb_build_object('Children', jsonb_agg(f.x))) - 'SearchVector') - 'ParentLocationId')
                    ELSE ((to_jsonb(t) - 'SearchVector') - 'ParentLocationId')
                END
            FROM "Locations" t
            LEFT JOIN build_hierarchey(t."Id", prefer_depth) AS f(x) ON true
            WHERE t."ParentLocationId" = location_id OR (location_id IS null AND t."ParentLocationId" IS null)
            GROUP BY t."Id", t."Name";  
        END IF; 
        RETURN;
    END;
    $$ LANGUAGE plpgsql

    END IF;
END $$;

错误

ERROR:  syntax error at or near "BEGIN"

LINE 8:  BEGIN

         ^

SQL state: 42601

Character: 286

1 个答案:

答案 0 :(得分:0)

问题在于定界符在IF条件内重复出现

DO $$
IF NOT EXISTS .....
$func$
     BEGIN
     SQL/FUNCTION/TRIGGER
END;$func$
END $$;