【问题标题】:EF core migration script error when using with postgresql与 postgresql 一起使用时出现 EF 核心迁移脚本错误
【发布时间】:2023-04-02 04:42:01
【问题描述】:

我有以下迁移脚本。如果我只运行 CREATE OR REPLACE FUNCTION 阻止它工作,我不知道为什么会出错..

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

【问题讨论】:

    标签: postgresql ef-core-2.2


    【解决方案1】:

    问题在于分隔符在 IF 条件内重复

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-15
      • 1970-01-01
      • 1970-01-01
      • 2013-12-27
      • 2018-05-15
      • 2018-08-09
      • 2018-05-21
      相关资源
      最近更新 更多