4

I realize, when talking about structures, obviously if you say "post hasMany comments" there are many records about comments relating to each post.

What I don't understand is what context these sort of terms are used in. I'm thinking mainly only in DBAL or talking about design. In any case, when one thing "hasMany" of another how and where does it show up?

2
  • 1
    Do you mean the exact term camel cased "hasMany", or the general English language usage of this phrase to describe relationships?
    – p.campbell
    Commented Jun 3, 2011 at 20:44
  • I think I mean the camel cased version, but honestly I am unsure. I've only used proprietary in-memory databases in the past. Commented Jun 3, 2011 at 20:55

1 Answer 1

6

In any case, when one thing hasMany of another how/where does it show up in application and database logic?

The only way I can think to answer this question is with another example of hasMany that may or may not answer your question.

an example of a hasMany using posts and comments

err, except I should have called the table "comment" but I think the point stands.

I don't know that this really helps you understand anymore than what you already had.

So I'll also share a query that might demonstrate how this works:

declare @var char(5)
set @var = 'query'

select
   c.id
  ,c.text
  ,c.author
from comment c
inner join post p
   on p.id = c.parentid

which we expect to return many rows, because one post generally has many comments (in theory, of course not all posts will have comments)


Update:

So here's the thing about relations: They are EASY to model via diagram (see the very first tool I pulled out to answer this question) but they are HARD to diagram via text. So instead of trying to define whether we mean 1:M or 0:N, we just write it in "near English" and say what we intend. Here's some alternatives:

one to one           hasOne               post hasOne author (not true for CW ;])
one to many          hasMany              The one in question
many to one          belongsTo            parent child, comments belongTo post
many to many         hasAndBelongsToMany  This describes a complex relationship
many to many to many hasManyAndBelongsToMany

As you see they can get quite complicated. This would ... once again! ... indicate a need to refactor your business logic into simpler and smaller chunks.

These can also be called "association types" and may be indicated by verbage like "WrittenBy" (a many to one) or "IsOwnedBy" (one to one? - context is key)

Before I walk away from the "hasMany" concept for the last time (well this is an update, more will likely be written) I want to mention that while it's useful for "plain English descriptions", it's also used in ActiveRecord styled installations. Take, for instance, Ruby. http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html The rest of this post (so far) is taken from that page:

class Project < ActiveRecord::Base
  belongs_to              :portfolio
  has_one                 :project_manager
  has_many                :milestones
  has_and_belongs_to_many :categories
end

This allows the ActiveRecord ORM to create the data model and to maintain the relationships as specified by the business logic within the program. But I don't write Ruby or Rails so that's as far as I can tell you, I just know that it exists there.

7
  • ahhh, @jcolebrand , you changed your name! was wondering who this new player was. Commented Jun 3, 2011 at 21:48
  • The demonstration helps 'at all' ;P but what is hasMany exactly? Is it just that you often see it in objects as a method to define that this query will be needed later? Is it just a way to 'talk' about the design? I'm still a little fuzzy on joins too so I will make sure I am visualizing this correctly when I get home later. thanks Commented Jun 3, 2011 at 21:51
  • It's a question of when I'd actually ever write "hasMany" and understanding why as opposed to say.. post gets comments. Though I think I understand it is fairly synonymous with "belongsTo" (in reverse) the understanding is roughly the same issue. Commented Jun 3, 2011 at 21:52
  • 2
    It's a relation, a term to describe the relationship of comments to posts so that you know there can be more than one comment per post. Otherwise post_id in the comments table would be unique Commented Jun 3, 2011 at 22:26
  • @GaretClaborn I hope the update helps you a little more.
    – jcolebrand
    Commented Jun 4, 2011 at 2:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.