English 中文(简体)
Can ActiveRecord HasMany/Belongs To use a non-PrimaryKey Column for the relation?
原标题:

I d like to have a parent-child hierarchy AND I d like to have the concept of a "series-id" on the parent.

So the parent (if you updated it) would expire the old unique key.. keep the series id and insert a second row.

I d like the children to "hang" off that series id rather than the unique id.

Do you know if it s possible for BelongsTo to link into a non primary key (yet unique via GUIDs) column?

Parent: UniqueId (guid), SeriesId (guid), Name, Description Child: UniqueId (guid), ParentId (always connects ot UniqueId-Above), Name, Description.

I know that foreign key relations really should have a "key" on one side of the relation however, by my GUIDs they are key-like.. but not really keys.

Its not a big deal but if I "expire" the parent by updating it s metadata i d like to avoid having to "copy" all the children into the new copy of the parent.

Am I trying to do something that can t happen?

最佳回答

There s an option for belongs_to that lets you specify the relationship:

:foreign_key
    Specify the foreign key used for the association. By default this is guessed to be the name of the association with an "_id" suffix. So a class that defines a belongs_to :person association will use "person_id" as the default :foreign_key. Similarly, belongs_to :favorite_person, :class_name => "Person" will use a foreign key of "favorite_person_id".
问题回答

暂无回答




相关问题
Many-To-Many Surrogate Key problem. Please help!

I have a many-to-many relationship with surrogate key The classes are: Insurer - InsurerSection - Section. InsurerSection has one extra attribute: active : bool. How do I access these bool ...

hql get objects where certain property is unique

I am trying to perform an hql query which returns a list of objects with distinct property value. Following is my pseudo code: string hql = @"select distinct m from Merchandise m where ...

nhibernate hql subquery performance

I have written an hql to support paging string hql = @"select distinct mr from MediaResource as mr where mr.Deleted= false ...

ASP.NET MVC - Castle ActiveRecord - Show SQL queries

I m using ASP.NET MVC with Castle ActiveRecord as my persistance layer. I want to know if it s possible to show the SQL queries being executed on my MySQL server. I know it s possible in a Web ...

热门标签