OK, first thing is that you should be using "backref" for this  
relationship, it only helps.

But secondly is, you are actually *setting* the relationship here by  
manipulating foreign key ids, which is usually not needed; the way to  
manipulate relations between ORM instances is by attribute/collection  
operations:

a1.t2s.append(a2)

or

a2.t1 = a1

now *if * want to work via the "ids" and foreign keys, you can; but  
you need to refresh those instances (using session.refresh()/ 
session.expire()) and/or clear your session (session.clear()) before  
proceeding.  in the first case in your test script, the lazy loader  
t2s hadnt been fired off yet which is why the first one works.


On Jul 27, 2007, at 8:07 AM, Michal Nowikowski wrote:

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