Luis Javier Peris | 17 May 01:55
Picon

RelatedJoin

'm working with SQLObject and I'm getting frustated because of lack of examples. First of all, I'm having problems creating tables with many-many relationships by means of RelatedJoin. I want to create and know how work the CRUD operations in the tables created by RelatedJoin operation. I have two tables I've created as follow:

class Term(SQLObject):
    name = StringCol(default=None)
    description = StringCol(default=None)
    vocabulary = ForeignKey('Vocabulary')
    term_questions = RelatedJoin('Question', joinColumn='question', otherColumn='term',
                                 intermediateTable='Term_questions')
Term.createTable(ifNotExists=True)

class Question(SQLObject):
    question_file = StringCol(default=None, length=255, unique=True)
    question_name = StringCol(default=None)
    question_type = StringCol(default=None)
    term_questions = RelatedJoin('Term', joinColumn='term', otherColumn='question',
                                 intermediateTable='Term_questions')
Question.createTable(ifNotExists=True)

So, I have a new table with two columns (term and question). How CRUD operations work?

I've tried to create a new instance of Term_questions table:
Term_questions(term=term.id, question=q.id), but the module Term_questions doesn't exist or if I try:
q = Question(question_file="file", question_name= "name", question_type="MultipleChoice")
term.term_questions = q.id
I get: AttributeError: can't set attribute

Can anybody help me?

Thanks in advance.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft 
Defy all challenges. Microsoft(R) Visual Studio 2008. 
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
Luke Opperman | 17 May 07:06
Picon

Re: RelatedJoin


First off, let me say I feel RelatedJoin and MultipleJoin should be deprecated
to be renamed RawRelatedJoin/RawMultipleJoin, there are two better alternatives
available since at least 0.8:
 * SQLRelatedJoin and SQLMultipleJoin
 * ManyToMany and OneToMany

Yes, the docs need a massive overhaul on this topic - I don't believe ManyToMany
and OneToMany are mentioned at all.

These both have the advantage over the Raw versions of returning SelectResults
instances: lazy, filterable etc result sets same as is returned by .select()

They differ in how you perform the C*UD operations you're asking about, in short
ManyToMany has .add(obj), .remove(obj), .create(kw) methods under obj.joinName,
whereas SQLRelatedJoin adds methods to the class: .addJoinName(obj) and
.removeJoinName(obj). If you choose to keep using RelatedJoin, it acts like
SQLRelatedJoin in this aspect.

I've put a doctest example of these and a little more up at
http://freehg.org/u/loppear/sqlobject_play/file/5cf57a8ce206/m2m_versus_sqljoin.py

- Luke

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft 
Defy all challenges. Microsoft(R) Visual Studio 2008. 
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Luis Javier Peris | 17 May 19:21
Picon

Re: RelatedJoin

Thanks for the example.

2008/5/17 Luke Opperman <loppear <at> gmail.com>:

First off, let me say I feel RelatedJoin and MultipleJoin should be deprecated
to be renamed RawRelatedJoin/RawMultipleJoin, there are two better alternatives
available since at least 0.8:
 * SQLRelatedJoin and SQLMultipleJoin
 * ManyToMany and OneToMany


Yes, the docs need a massive overhaul on this topic - I don't believe ManyToMany
and OneToMany are mentioned at all.

These both have the advantage over the Raw versions of returning SelectResults
instances: lazy, filterable etc result sets same as is returned by .select()

They differ in how you perform the C*UD operations you're asking about, in short
ManyToMany has .add(obj), .remove(obj), .create(kw) methods under obj.joinName,
whereas SQLRelatedJoin adds methods to the class: .addJoinName(obj) and
.removeJoinName(obj). If you choose to keep using RelatedJoin, it acts like
SQLRelatedJoin in this aspect.

I've put a doctest example of these and a little more up at
http://freehg.org/u/loppear/sqlobject_play/file/5cf57a8ce206/m2m_versus_sqljoin.py

- Luke



-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft 
Defy all challenges. Microsoft(R) Visual Studio 2008. 
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
Oleg Broytmann | 17 May 17:42
X-Face
Picon
Favicon

Re: RelatedJoin

On Sat, May 17, 2008 at 01:59:03AM +0200, Luis Javier Peris wrote:
> 'm working with SQLObject and I'm getting frustated because of lack of
> examples. First of all, I'm having problems creating tables with
> many-many relationships
> by means of RelatedJoin. I want to create and know how work the CRUD
> operations in the tables created by RelatedJoin operation. I have two tables
> I've created as follow:
> 
> class Term(SQLObject):
>     name = StringCol(default=None)
>     description = StringCol(default=None)
>     vocabulary = ForeignKey('Vocabulary')
>     term_questions = RelatedJoin('Question', joinColumn='question',
> otherColumn='term',
>                                  intermediateTable='Term_questions')
> Term.createTable(ifNotExists=True)
> 
> class Question(SQLObject):
>     question_file = StringCol(default=None, length=255, unique=True)
>     question_name = StringCol(default=None)
>     question_type = StringCol(default=None)
>     term_questions = RelatedJoin('Term', joinColumn='term',
> otherColumn='question',
>                                  intermediateTable='Term_questions')
> Question.createTable(ifNotExists=True)
> 
> So, I have a new table with two columns (term and question). How CRUD
> operations work?
> 
> I've tried to create a new instance of Term_questions table:
> Term_questions(term=term.id, question=q.id), but the module Term_questions
> doesn't exist

   It doesn't exist. If you want to explicitly declare it:
http://sqlobject.org/FAQ.html#how-can-i-define-my-own-intermediate-table-in-my-many-to-many-relationship

> or if I try:
> q = Question(question_file="file", question_name= "name",
> question_type="MultipleChoice")
> term.term_questions = q.id
> I get: AttributeError: can't set attribute

   term.term_questions is a list. If want to add an object to the join, use
add*() methods:

term.addQuestion(q)

   or

q.addTerm(term)

Oleg.
--

-- 
     Oleg Broytmann            http://phd.pp.ru/            phd <at> phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft 
Defy all challenges. Microsoft(R) Visual Studio 2008. 
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Luis Javier Peris | 17 May 19:24
Picon

Re: RelatedJoin

Thanks for the accurate answer, it have a lot of sense to have an add method but I didn't know that it existed.

2008/5/17 Oleg Broytmann <phd <at> phd.pp.ru>:
On Sat, May 17, 2008 at 01:59:03AM +0200, Luis Javier Peris wrote:
> 'm working with SQLObject and I'm getting frustated because of lack of
> examples. First of all, I'm having problems creating tables with
> many-many relationships
> by means of RelatedJoin. I want to create and know how work the CRUD
> operations in the tables created by RelatedJoin operation. I have two tables
> I've created as follow:
>
> class Term(SQLObject):
>     name = StringCol(default=None)
>     description = StringCol(default=None)
>     vocabulary = ForeignKey('Vocabulary')
>     term_questions = RelatedJoin('Question', joinColumn='question',
> otherColumn='term',
>                                  intermediateTable='Term_questions')
> Term.createTable(ifNotExists=True)
>
> class Question(SQLObject):
>     question_file = StringCol(default=None, length=255, unique=True)
>     question_name = StringCol(default=None)
>     question_type = StringCol(default=None)
>     term_questions = RelatedJoin('Term', joinColumn='term',
> otherColumn='question',
>                                  intermediateTable='Term_questions')
> Question.createTable(ifNotExists=True)
>
> So, I have a new table with two columns (term and question). How CRUD
> operations work?
>
> I've tried to create a new instance of Term_questions table:
> Term_questions(term=term.id, question=q.id), but the module Term_questions
> doesn't exist

  It doesn't exist. If you want to explicitly declare it:
http://sqlobject.org/FAQ.html#how-can-i-define-my-own-intermediate-table-in-my-many-to-many-relationship

> or if I try:
> q = Question(question_file="file", question_name= "name",
> question_type="MultipleChoice")
> term.term_questions = q.id
> I get: AttributeError: can't set attribute

  term.term_questions is a list. If want to add an object to the join, use
add*() methods:

term.addQuestion(q)

  or

q.addTerm(term)

Oleg.
--
    Oleg Broytmann            http://phd.pp.ru/            phd <at> phd.pp.ru
          Programmers don't die, they just GOSUB without RETURN.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft 
Defy all challenges. Microsoft(R) Visual Studio 2008. 
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss
Oleg Broytmann | 17 May 20:44
X-Face
Picon
Favicon

Re: RelatedJoin

On Sat, May 17, 2008 at 07:24:57PM +0200, Luis Javier Peris wrote:
> Thanks for the accurate answer, it have a lot of sense to have an add method
> but I didn't know that it existed.
> 
> 2008/5/17 Oleg Broytmann <phd <at> phd.pp.ru>:
> > term.addQuestion(q)
> >
> >   or
> >
> > q.addTerm(term)

   They are added automatically, and the names depend on the names of the
classes.

Oleg.
--

-- 
     Oleg Broytmann            http://phd.pp.ru/            phd <at> phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft 
Defy all challenges. Microsoft(R) Visual Studio 2008. 
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

Gmane