Michael Hipp wrote:
> I don't understand how to use the "two" fields created in a
> ManyToOne relationship?
>
> From the tutorial, as below, Elixir will create (in Python) two
> fields: 'director' and 'director_id'. How are they used? And how
> are they kept in sync?
So, you aren't actually creating two "fields". You are creating a
single column in the database, representing the foreign key to the
referenced table. In this case, that column is "director_id." The
`director_id` attribute will be populated with the foreign key
to the referenced row in your `director` table. The `director`
attribute on your `Movie` entity will use that foreign key to load
the referenced row from the `director` table into a `Director`
object.
SQLAlchemy will keep these fields in sync, but not until after the
object is flushed back to the database. In your example, if you add
a "flush" of the objects to the database, then the synchronization
will happen:
from elixir import *
class Movie(Entity):
director = ManyToOne('Director')
class Director(Entity):
movies = OneToMany('Movie')
setup_all()
metadata.bind = 'sqlite:///'
metadata.create_all()
d = Director(name="George")
m = Movie(director=d)
print m.director, m.director_id
d.flush()
m.flush()
print m.director, m.director_id
The output will be like this:
<__main__.Director object at 0x66ee50> None
<__main__.Director object at 0x66ee50> 1
SQLAlchemy can't set the foreign key until the objects have been
flushed, since it doesn't yet *have* an id. Once the flush occurs,
SQLAlchemy "links" everything back together again.
> This seems inconsistent. The 'director_id' field makes it appear
> if there is no director assigned to 'm'. But 'director' clearly
> holds a Director object.
>
> Is this not dangerous? When do you use one or the other?
>
> If 'director_id' is just a shortcut, wouldn't it be more correct
> and safe to just refer to 'm.director.id'?
>
> Note that this is complicated, IMHO, by the fact that m.table.c
> contains 'director_id' rather than 'director' (the more useful
> attr).
>
> I'm obviously just not getting it, can someone expound on this a
> bit?
You're just confounding the database schema with the object's
attributes. They aren't a direct one-to-one mapping. The
`director_id` field is the database's way of knowing how one row
relates to another. The `director` *attribtue* isn't stored in the
database at all, its inferred by SQLAlchemy based upon the foreign
key.
I hope this clears things up for you. It sounds like you're just
new to the world of relational databases. You'll get used to it, I
assure you :)
--
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
-~----------~----~----~----~------~----~------~--~---