Skip to content

人大金仓 AsTreeCte 递归删除报错 #2250

Description

@guangcaia

问题描述及重现代码:

ERROR:  recursive query "as_tree_cte" column 1 has type integer in non-recursive term but type bigint overall
LINE 4: SELECT  0 as cte_level, a."Id", a."ParentId", ...
                ^
HINT:  Cast the output of the non-recursive term to the correct type.
public class ApplicationMenu
{
    [Column(IsPrimary = true, IsIdentity = true)]
    public int Id { get; set; }

    [Column(IsNullable = true)]
    public int? ParentId { get; set; }

   // 其他字段省略...

    [Navigate(nameof(ParentId))]
    public ApplicationMenu? Parent { get; set; }

    [Navigate(nameof(ParentId))]
    public List<ApplicationMenu>? Children { get; set; }
}

public interface IMenuService
{
    Task DeleteAsync(int menuId);
}

internal class MenuService(IBaseRepository<ApplicationMenu> repo) : IMenuService
{
    public Task DeleteAsync(int menuId)
    {
        return repo.Where(x => x.Id == menuId)
            .AsTreeCte()
            .ToDelete()
            .ExecuteAffrowsAsync();
    }
}

实际生成的 SQL

WITH RECURSIVE "as_tree_cte"
as
(
SELECT  0 as cte_level, a."Id", a."ParentId"
FROM "ApplicationMenu" a
WHERE (a."Id" = 1)
union all
SELECT wct1.cte_level + 1 as cte_level, wct2."Id", wct2."ParentId"
FROM "as_tree_cte" wct1
INNER JOIN "ApplicationMenu" wct2 ON wct2."ParentId" = wct1."Id"
)
DELETE FROM "ApplicationMenu" WHERE ("Id" in (select * from (SELECT a."Id"
FROM "as_tree_cte" a) ftb_del))

期望的 SQL

WITH RECURSIVE "as_tree_cte"
as
(
SELECT  0::BIGINT as cte_level, a."Id", a."ParentId"
FROM "ApplicationMenu" a
WHERE (a."Id" = 1)
union all
SELECT wct1.cte_level + 1 as cte_level, wct2."Id", wct2."ParentId"
FROM "as_tree_cte" wct1
INNER JOIN "ApplicationMenu" wct2 ON wct2."ParentId" = wct1."Id"
)
DELETE FROM "ApplicationMenu" WHERE ("Id" in (select * from (SELECT a."Id"
FROM "as_tree_cte" a) ftb_del))

数据库版本

KingbaseES V009R001C010 database_mode=mysql

安装的Nuget包

3.5.310

.net framework/. net core? 及具体版本

net10.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions