Hi again Oleg.
This must be a problem between my chair and my keyboard. :/
Anyway, I managed to have the results I wanted with the following code:
#############################
# -*- coding: utf-8 -*-
import sqlobject
class A (sqlobject.SQLObject):
name = sqlobject.StringCol ()
b = sqlobject.RelatedJoin ('B')
class B (sqlobject.SQLObject):
name = sqlobject.StringCol ()
a = sqlobject.RelatedJoin ('A', intermediateTable='b_a')
aa = sqlobject.ManyToMany ('A', intermediateTable='b_a')
if __name__ == "__main__":
# Use the same DB connection for all the thread.
sqlobject.sqlhub.threadConnection = sqlobject.connectionForURI
('sqlite:///tmp/testa.db')
A.createTable ()
B.createTable ()
a1 = A (name='a1')
a2 = A (name='a2')
a3 = A (name='a2')
b1 = B (name='b1')
a1.addB (b1)
b1.addA (a2)
b1.addA (a3)
b = B.selectBy (name='b1').getOne ()
print ("a1 = " + str (a1))
print ("b = " + str (b))
print ("b.a = " + str (b.a))
print ("b.aa = " + str (b.aa))
#############################
Running it results in the following output:
a1 = <A 1 name='a1'>
b = <B 1 name='b1'>
b.a = [<A 2 name='a2'>, <A 3 name='a2'>]
b.aa = SELECT a.id, a.name FROM a, b_a WHERE (((a.id) = (b_a.a_id))
AND ((b_a.b_id) = (1)))
The scheme in the database is:
CREATE TABLE a (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE a_b (a_id INT NOT NULL, b_id INT NOT NULL);
CREATE TABLE b (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE b_a (b_id INT NOT NULL, a_id INT NOT NULL);
Which is what I expected it to be.
The trick I'm using is defining ManyToMany and RelatedJoin to use the
same intermediate table. It's not pretty, but it works.
Do I really have to use such trick? Or am I miss reading the documentation?
Thanks in advance,
Miguel Tavares
2009/8/12 Oleg Broytmann <ph...@ph...>:
> On Wed, Aug 12, 2009 at 02:43:50PM +0100, Miguel Tavares wrote:
>> a1 = A ()
>> a2 = A ()
>> a3 = A ()
>> b1 = B ()
>>
>> a1.add (b1)
>> b1.add (a2)
>> b1.add (a3)
>
> a1 = A()
> a2 = A()
> a3 = A()
> b1 = B()
>
> a1.addB1(b1)
> b1.addA1(a1)
> b1.addA2(a2)
>
> 5/QueryR : INSERT INTO a VALUES (NULL)
> 6/QueryR : SELECT NULL FROM a WHERE ((a.id) = (1))
> 7/QueryR : INSERT INTO a VALUES (NULL)
> 8/QueryR : SELECT NULL FROM a WHERE ((a.id) = (2))
> 9/QueryR : INSERT INTO a VALUES (NULL)
> 10/QueryR : SELECT NULL FROM a WHERE ((a.id) = (3))
> 11/QueryR : INSERT INTO b VALUES (NULL)
> 12/QueryR : SELECT NULL FROM b WHERE ((b.id) = (1))
> 13/QueryR : INSERT INTO a1_b1 (a_id, b_id) VALUES (1, 1)
> 14/QueryR : INSERT INTO a1_b1 (b_id, a_id) VALUES (1, 1)
> 15/QueryR : INSERT INTO a2_b2 (b_id, a_id) VALUES (1, 2)
>
> M?
>
> Oleg.
> --
> Oleg Broytmann http://phd.pp.ru/ ph...@ph...
> Programmers don't die, they just GOSUB without RETURN.
>
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> trial. Simplify your report design, integration and deployment - and focus on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now. http://p.sf.net/sfu/bobj-july
> _______________________________________________
> sqlobject-discuss mailing list
> sql...@li...
> https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
>
|