MonoRail ActiveRecord - UPDATE 语句与 FOREIGN KEY SAME TABLE 冲突
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需要这个:
删除此属性:
当您使用FindAllByProperty()(或任何其他NHibernate/ActiveRecord标准)时,您使用的属性名称是映射的属性,而不是表列,因此正确的用法是
FindAllByProperty("Parent.Id",parentId)
,而不是FindAllByProperty("ParentId",parentId)
。 (假设类别具有 PK 属性Id
)如果您在执行查询时看到 UPDATE,通常意味着您正在使用自动会话刷新,并且一个或多个会话实体已脏,因此 NHibernate 必须在查询之前刷新会话并更新这些脏实体,以确保不会得到过时的结果。
小问题:删除
DetachedCriteria
,它在那里没有被使用。You only need this one:
Remove this property:
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 isFindAllByProperty("Parent.Id", parentId)
, notFindAllByProperty("ParentId", parentId)
. (assuming Category has a PK propertyId
)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.