On Mar 31, 2008, at 3:33 PM, Nebur wrote:

>
> I've first time tried so set up a Many To One relation in one table.
> After flushing, the foreign key reference is set on the wrong side,
> and I failed to tell the mapper what I want.
> The example has a "Man" table where each son has a reference to its
> father:
> ++++++++++++++++++++++++++++++
>
>
> class Man(object):
>       def __init__(self,name):
>               self.name = name
>
>       def __str__(self): return "%s#%s'%s'"%
> (self.__class__.__name__,self.id,self.name)
>       def __repr__(self): return self.__str__()
>
>
> db = create_engine('sqlite:///:memory:')
> orms = create_session()
> metadata = MetaData(db)
>
> t_man = Table("man",metadata,
>       Column("id",Integer,primary_key=True),
>       Column("name",String(50)),
>       Column("father_id",Integer,ForeignKey("man.id")),
>       )
> metadata.create_all()
>
> mapper(Man, t_man, properties = {
>       "father":relation(
>               Man,
>               uselist=False,
>               #remote_side=t_man.c.father_id # same error with remote_side 
> scalar
>               #remote_side=[t_man.c.father_id] # same error with remote_side 
> []
>               )
>       })
>

self referential many-to-one looks like:

mapper(Class, table, properties={
        'parent':relation(Class, remote_side=table.c.id)
})

"id" is remote side for many-to-one since the left side of the  
equation holds the "parent_id" column, the right side holds the "id".   
Heres some ASCII art we used at the Pycon tutorials to illustrate  
"remote_side":  http://dpaste.com/42425/



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" 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/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to