Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top