dotnetcore / dotnetcore/FreeSql

人大金仓 AsTreeCte 递归删除报错

Open
#2,250 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
4.4k
Forks
910
PR merge metrics
No merged PRs in 30d

Description

#### 问题描述及重现代码:
```txt
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.
```
```c#
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? Children { get; set; }
}

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

internal class MenuService(IBaseRepository repo) : IMenuService
{
public Task DeleteAsync(int menuId)
{
return repo.Where(x => x.Id == menuId)
.AsTreeCte()
.ToDelete()
.ExecuteAffrowsAsync();
}
}
```
实际生成的 SQL
```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
```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

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at the AsTreeCte().ToDelete() path that generates the recursive SQL, using the supplied C# model and KingbaseES reproduction. Compare the generated and expected queries, then add a regression test for the recursive delete case. Done means the cte_level type is compatible across both recursive terms and the delete succeeds on KingbaseES.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.