MonoRail ActiveRecord - The UPDATE statement conflicted with the FOREIGN KEY SAME TABLE
Posted
by Justin
on Stack Overflow
See other posts from Stack Overflow
or by Justin
Published on 2010-05-26T20:48:12Z
Indexed on
2010/05/26
20:51 UTC
Read the original article
Hit count: 359
castle-monorail
Hey all,
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
© Stack Overflow or respective owner