Jonathan LaCour wrote:
> 
> 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:

Thanks. I took your more complete example and added to it. It illustrates the 
part that is (was?) throwing me:

from elixir import *

class Movie(Entity):
     director = ManyToOne('Director')

class Director(Entity):
     name = Field(Unicode(60))
     movies = OneToMany('Movie')

metadata.bind = 'sqlite:///'
setup_all()
metadata.create_all()

d = Director(name=u"George")
m = Movie(director=d)

print '---------------------------------'
print m.director, m.director_id  # Inconsistent
d.flush()
m.flush()
print m.director, m.director_id  # Consistent

print '---------------------------------'
# Down with the director!
m.director_id = None

print m.director, m.director_id  # Inconsistent
d.flush()
m.flush()
print m.director, m.director_id  # Inconsistent!!!

print '---------------------------------'
# The correct way...
m = Movie(director=d)
d.flush()
m.flush()
print m.director, m.director_id  # Consistent

# Down with the director!
m.director = None  # Don't touch the '_id' thing
print m.director, m.director_id  # Inconsistent
d.flush()
m.flush()
print m.director, m.director_id  # Consistent

Output:====================================================
---------------------------------
<__main__.Director object at 0x00F3BA70> None
<__main__.Director object at 0x00F3BA70> 1
---------------------------------
<__main__.Director object at 0x00F3BA70> None
<__main__.Director object at 0x00F3BA70> None
---------------------------------
<__main__.Director object at 0x00F3BA70> 1
None 1
None None

I'm still struggling with the idea of the director and director_id being in an 
inconsistent state until flush(). I'll *definitely* have to remember that. 
Seems like it could lead to many hard to find bugs.

In that middle block if I try to get rid of the Director by setting director_id 
to None, the object never comes back into a consistent state even after 
flush(). I guess the dictum is to never-ever mess with the "director_id" thing 
and consider it read-only?

Sorry for being so verbose.

Thanks,
Michael

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to