MonoRail ActiveRecord - UPDATE 语句与 FOREIGN KEY SAME TABLE 冲突

发布于 2024-09-02 17:37:23 字数 1204 浏览 3 评论 0原文

我是 MonoRail 和 ActiveRecord 的新手,并且继承了一个需要修改的应用程序。我有一个类别表(Id、名称),并且添加了一个 ParentId FK,它引用同一个表中的 Id,以便您可以拥有父/子类别关系。

我尝试将其添加到我的类别模型类中:

[Property] 
public int ParentId { get; set; }

我也尝试这样做:

[BelongsTo("ParentId")] 
public Category Parent { get; set; }

它工作得很好:

public static Category[] GetParentCategories() 
        { 
            var criteria = DetachedCriteria.For<Core.Models.Category>(); 
            return (FindAllByProperty("ParentId", null)); 
        }

当我调用一个方法来获取所有父类别(它们有一个 null ParentId)时,它工作正常:但是,当我调用一个方法来获取所有父类别时, 特定类别中的子类别时,会出错:

public static Category[] GetChildCategories(int parentId) 
        { 
            var criteria = DetachedCriteria.For<Core.Models.Category>(); 
            return (FindAllByProperty("ParentId", parentId)); 
        }

错误是:

“UPDATE 语句与 FOREIGN KEY SAME TABLE 约束 \"FK_Category_ParentId\" 冲突。冲突发生在数据库 \"UCampus\"、表 \" 中dbo.Category\", 'Id' 列。\r\n语句已终止。"

我将parentId 参数硬编码为 1,并且我 100% 确定它作为 id 存在类别表,所以我不知道为什么会出现此错误。另外,我正在进行选择,而不是更新,那么这里发生了什么?

感谢

贾斯汀对此的任何意见

I'm new to MonoRail and ActiveRecord and have inherited an application that I need to modify. I have a Category table (Id, Name) and I'm adding a ParentId FK which references the Id in the same table so that you can have parent/child category relationships.

I tried adding this to my Category model class:

[Property] 
public int ParentId { get; set; }

I also tried doing it this way:

[BelongsTo("ParentId")] 
public Category Parent { get; set; }

When I call a method to get all parent categories (they have a null ParentId), it works fine:

public static Category[] GetParentCategories() 
        { 
            var criteria = DetachedCriteria.For<Core.Models.Category>(); 
            return (FindAllByProperty("ParentId", null)); 
        }

However, when I call a method to get all child categories within a specific category, it errors out:

public static Category[] GetChildCategories(int parentId) 
        { 
            var criteria = DetachedCriteria.For<Core.Models.Category>(); 
            return (FindAllByProperty("ParentId", parentId)); 
        }

The error is:

"The UPDATE statement conflicted with the FOREIGN KEY SAME TABLE constraint \"FK_Category_ParentId\". The conflict occurred in database \"UCampus\", table \"dbo.Category\", column 'Id'.\r\nThe statement has been terminated."

I'm hard-coding in the parentId parameter as 1 and I'm 100% sure it exists as an id in the Category table so I don't know why it'd give this error. Also, I'm doing a select, not an update, so what is going on here??

Thanks for any input on this,

Justin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亣腦蒛氧 2024-09-09 17:37:23

您只需要这个:

[BelongsTo("ParentId")] 
public Category Parent { get; set; }

删除此属性:

[Property] 
public int ParentId { get; set; }

当您使用FindAllByProperty()(或任何其他NHibernate/ActiveRecord标准)时,您使用的属性名称是映射的属性,而不是表列,因此正确的用法是 FindAllByProperty("Parent.Id",parentId),而不是 FindAllByProperty("ParentId",parentId)。 (假设类别具有 PK 属性 Id

如果您在执行查询时看到 UPDATE,通常意味着您正在使用自动会话刷新,并且一个或多个会话实体已脏,因此 NHibernate 必须在查询之前刷新会话并更新这些脏实体,以确保不会得到过时的结果。

小问题:删除 DetachedCriteria,它在那里没有被使用。

You only need this one:

[BelongsTo("ParentId")] 
public Category Parent { get; set; }

Remove this property:

[Property] 
public int ParentId { get; set; }

When you use FindAllByProperty() (or any other NHibernate/ActiveRecord criteria), the property name you use is the mapped property, not the table column, so the correct usage is FindAllByProperty("Parent.Id", parentId), not FindAllByProperty("ParentId", parentId). (assuming Category has a PK property Id)

If you see an UPDATE when you're doing a query it usually means that you're using automatic session flushing and one or more of your session entities is dirty, so NHibernate has to flush the session and UPDATE these dirty entities before querying to make sure you don't get stale results.

Minor nitpick: remove the DetachedCriteria, it's not being used there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文