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
[]
)
})
# create two men:
james = Man("James")
harry = Man("Harry")
harry.father = james
orms.save(harry)
assert harry.father is james
assert james.father is None
assert harry.father_id is None
assert james.father_id is None
orms.flush()
# These both are as expected:
assert harry.father is james
assert james.father is None
# Error: son has no father id:
assert harry.father_id==harry.father.id # harry.father_id is None
# Error: father has father_id:
assert james.father_id is None # james.father_id==harry.id
# Both wrong father_ids are reflected in the database.
+++++++++++++++++++
Father attribute and father_id are inconsistent and remote_side
changes nothing.
All SA 0.4.x versions and 0.3.10,0.3.7 behave the same way. So I
consider I've misused the mapping somehow... what's wrong here ?
Thank you.
Ruben
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---