The problem is that when you run your last query, SA sees that there is
still an existing T1 object (with the right primary key) in memory (both
in the session and in your 'a1' and 'ra1' variables). It deliberately
doesn't update them.

To get the behaviour you expect, you need to get rid of previous
references to your objects. Try adding these lines before the last
query:

# Clear the session
s.clear()
# Get rid of local references to T1 and T2 instances
del a1, a2, a22, ra1

This behaviour is documented here:

http://www.sqlalchemy.org/docs/unitofwork.html#unitofwork_identitymap

You may also be interested in the refresh/expire methods of session:

http://www.sqlalchemy.org/docs/unitofwork.html#unitofwork_api_refreshexp
ire

Hope that helps,

Simon

> -----Original Message-----
> From: sqlalchemy@googlegroups.com 
> [mailto:[EMAIL PROTECTED] On Behalf Of Michal Nowikowski
> Sent: 27 July 2007 13:08
> To: sqlalchemy
> Subject: [sqlalchemy] not updated relation one-to-many
> 
> 
> Hello
> 
> I've following problem. I've two tables (t1, t2) and relation t1 (one)
> - t2 (many):
>   mapper(T1, t1, properties={"t2s": relation(T2, lazy=False)})
>   mapper(T2, t2, properties={"t1": relation(T1, lazy=False)})
> 
> When I add row to t1, then to t2, and then run query for first row in
> t1, I see one
> element in collection t2s - it is ok.
> Then when I add second row to t2, the collection in t1 object is not
> updated.
> It still contains only one element.
> 
> Example below.
> 
> Could you tell me how to refresh collection in one-to-many relation???
> 
> Regards
> Michal Nowikowski
> 
> 
> from sqlalchemy import *
> 
> md = MetaData('sqlite:///a.db', echo=False)
> t1 = Table("t1", md,
>            Column("id", Integer, primary_key=True),
>            Column("version", Integer))
> 
> t2 = Table("t2", md,
>            Column("id", Integer, primary_key=True),
>            Column("name", String),
>            Column("t1_id", Integer, ForeignKey("t1.id")))
> 
> md.create_all()
> s = create_session()
> 
> class T1(object):
>     pass
> class T2(object):
>     pass
> 
> mapper(T1, t1, properties={"t2s": relation(T2, lazy=False)})
> mapper(T2, t2, properties={"t1": relation(T1, lazy=False)})
> 
> a1 = T1()
> s.save(a1)
> s.flush()
> 
> a2 = T2()
> a2.t1_id = a1.id
> a2.name = "AAA"
> s.save(a2)
> s.flush()
> 
> ra1 = s.query(T1).first()
> print [ a.name for a in ra1.t2s ]
> 
> a22 = T2()
> a22.t1_id = a1.id
> a22.name = "BBB"
> s.save(a22)
> s.flush()
> 
> rra1 = s.query(T1).first()
> print [ a.name for a in rra1.t2s ]
> 
> 
> > 
> 

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
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