tags:

views:

37

answers:

1

I was wondering if there was such thing as 'belongs to' relationship, and what was its function, or if it was just the opposite to 'has one'?

The question arised when building an ORM, and wanted to find a way to determine when a entity instance should be automatically deleted, eg:

User 'has many' Thread

Thread 'has many' Comment

Thread 'has one' User

Comment 'has one' Thread

Suppose you delete a user instance. Its related thread instances should remain intact. But if you delete a thread instance, its comments should be deleted.

With the above schema, the ORM can't tell when to delete and when not to. But if I use 'belong to', it could be a solution:

User 'has many' Thread

Thread 'has many' Comment

Thread 'has one' User

Comment 'belongs to one' Thread

Does this make sense? It is possible for the ORM, but does ERD cover this scenario? The one-to-one/one-to-many connections don't seem to be enough.

Any thoughts?

A: 

The general implementations that I have seen all provide for a "belongs to" relationship. Technically this will exist in any "Has many" relationship because the foreign key will exist in the child table.

A definition from Kohana's ORM class documentation suggests that whether a one-to-one relationship is "has one" or "belongs to" depends on the location of the foreign key field. If it is in the same table as the core model then it is a "belongs to" relationship. If it is in another table then it is a "has one" relationship.

Noah Goodrich