Michael Hipp wrote:
> As in the Elixir tutorial, is it necessary to specify both sides
> of the "parent/child" relationship?
>
> class Movie(Entity):
> director = ManyToOne('Director')
>
> class Director(Entity):
> movies = OneToMany('Movie')
>
> What happens if you leave off the 'OneToMany' line?
Everything will be exactly the same, except you'll not be able to
fetch all `Movie` objects for a `Director` simply by accessing the
`movies` attribute.
Essentially, a `ManyToOne` does two things:
1. Creates a foreign key on the table for the entity to the
table for the *related* entity. In this case, this column will be
called `director_id`.
2. Sets up a "relationship" between the entity and the referenced
entity (in this case `director`) which will automatically load
or set the referenced `Director` object using the aforementioned
foreign key.
Now, a `OneToMany` represents the other side of this relationship,
so it only creates a "relationship" and doesn't affect your schema
in any way.
> (This is confusing as when doing this sort of stuff in SQL, I
> normally just put the F.K. spec in the child and have it point to
> the id of the parent. With the parent none the wiser.)
Sure, and you're doing exactly the same thing here, through the
`ManyToOne` relationship. The `OneToMany` is just the other side of
the relationship that you can use to load related objects, perform
queries based upon the relationship, and add related objects from
the other side.
Personally, I *always* specify both sides of the relationship to
make it absolutely clear in my code which objects are related to
which other objects, and exactly how they are related. It also makes
it easy to do things like eager loading and filtering based upon
relationships on either side.
I hope this clears things up for you a bit :)
--
Jonathan LaCour
http://cleverdevil.org
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"SQLElixir" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---