TurboGears: best practices for SELECTing

1 view
Skip to first unread message

James Brady

unread,
Oct 10, 2007, 7:11:13 AM10/10/07
to sqlalchemy
Hello all,
I'm a newcomer to SA, using it as the ORM for TurboGears at the
moment.

Is there documentation/tutorials/recipes somewhere about how to
properly use SA from TurboGears?

The problem I'm hitting at the moment is how to properly select simple
objects... There seems to be two main approaches, for example:
session.query(Ownership).select() or
ownership_table.select().execute()

With the first approach, I get
InvalidRequestError: Parent instance <class 'tnf.model.Ownership'> is
not bound to a Session, and no contextual session is established; ...
which I'm not sure how tackle - there's no mention I can find of when
and where create_session should be called...

The the second approach, the SELECTs work fine, but then I'm working
with RoxProxy objects, rather than my models, which don't have the
relations and back-references I've set up and that I obviously need.

Can anyone answer these specific questions or point me in the
direction of some further documentation?

Thanks,
James

Marco Mariani

unread,
Oct 10, 2007, 9:12:34 AM10/10/07
to sqlal...@googlegroups.com
James Brady ha scritto:

> The problem I'm hitting at the moment is how to properly select simple
> objects... There seems to be two main approaches, for example:
> session.query(Ownership).select() or
> ownership_table.select().execute()
>
> With the first approach, I get
> InvalidRequestError: Parent instance <class 'tnf.model.Ownership'> is
> not bound to a Session, and no contextual session is established; ...
> which I'm not sure how tackle - there's no mention I can find of when
> and where create_session should be called...
>

Did you call assign_mapper on Ownership?

> Can anyone answer these specific questions or point me in the
> direction of some further documentation?
>

You should be able to call Ownership.query() just fine, if you have a
working mapper.

Lukasz Szybalski

unread,
Oct 10, 2007, 9:17:26 AM10/10/07
to sqlal...@googlegroups.com
On 10/10/07, James Brady <james.co...@gmail.com> wrote:
>
> Hello all,
> I'm a newcomer to SA, using it as the ORM for TurboGears at the
> moment.
>
> Is there documentation/tutorials/recipes somewhere about how to
> properly use SA from TurboGears?
>
> The problem I'm hitting at the moment is how to properly select simple
> objects... There seems to be two main approaches, for example:

Read over these examples. Hopefully that answers your question.

> session.query(Ownership).select() or
http://lucasmanual.com/mywiki/TurboGears#head-6f83a62f8c04cde09ea95b375a4041ce0817c661
> ownership_table.select().execute()
http://lucasmanual.com/mywiki/TurboGears#head-eb3ffb8d71d898537ec2538152d00dd243d5241e

>
> With the first approach, I get
> InvalidRequestError: Parent instance <class 'tnf.model.Ownership'> is
> not bound to a Session, and no contextual session is established; ...
> which I'm not sure how tackle - there's no mention I can find of when
> and where create_session should be called...
>
> The the second approach, the SELECTs work fine, but then I'm working
> with RoxProxy objects, rather than my models, which don't have the
> relations and back-references I've set up and that I obviously need.
>
> Can anyone answer these specific questions or point me in the
> direction of some further documentation?
>
> Thanks,
> James
>
>
> >
>


--
--
TurboGears from start to finish:
http://www.lucasmanual.com/mywiki/TurboGears

Nebur

unread,
Oct 11, 2007, 3:05:57 AM10/11/07
to sqlalchemy

> Can anyone answer these specific questions or point me in the
> direction of some further documentation?
I assume that the SA doc, chapter "object relational tutorial" does
best clearify how to create and use a session.
http://www.sqlalchemy.org/docs/04/ormtutorial.html
Or are there some shortcomings that experienced users cannot see
anymore ?
Regards,
Ruben

James Brady

unread,
Oct 11, 2007, 5:14:52 AM10/11/07
to sqlalchemy
On Oct 11, 8:05 am, Nebur <nospam1.reifenb...@gmx.de> wrote:
> > Can anyone answer these specific questions or point me in the
> > direction of some further documentation?
>
> I assume that the SA doc, chapter "object relational tutorial" does
> best clearify how to create and use a session.http://www.sqlalchemy.org/docs/04/ormtutorial.html

> Or are there some shortcomings that experienced users cannot see
> anymore ?

Hi Ruben,
I'm actually using version 0.3, so hadn't seen that tutorial - that is
the sort of thing I was looking for! To be honest, the hole in
documentation seems to be on the TurboGears side, which currently has
a lot more on SQLObject...

So it seems from that tutorial that session.query... is the way to go
for these selects, and I need to learn how to manage the sessions
properly!

Thanks,
James

James Brady

unread,
Oct 11, 2007, 5:54:06 AM10/11/07
to sqlalchemy

Hi Marco, assign_mapper did help - the relations are now accessible
through my models, and the query syntax is nicer than pure SA as well
in my opinion.

However, I'm using the identity framework (part of TurboGears) which
unfortunately doesn't play nicely with assign_mapper... shame!

James

Marco Mariani

unread,
Oct 11, 2007, 5:57:59 AM10/11/07
to sqlal...@googlegroups.com
James Brady ha scritto:

> Hi Marco, assign_mapper did help - the relations are now accessible
> through my models, and the query syntax is nicer than pure SA as well
> in my opinion.
>
> However, I'm using the identity framework (part of TurboGears)

Me too.


> which unfortunately doesn't play nicely with assign_mapper... shame!
>

What do you mean?

Florent Aide

unread,
Oct 11, 2007, 8:01:26 AM10/11/07
to sqlal...@googlegroups.com
On 10/11/07, James Brady <james.co...@gmail.com> wrote:
> > Did you call assign_mapper on Ownership?
> >

[...]

>
> However, I'm using the identity framework (part of TurboGears) which
> unfortunately doesn't play nicely with assign_mapper... shame!
>

If you use SA 0.3.10 I would advise you to use mapper (from
sqlalchemy.orm import mapper) instead of assign_mapper.
If you could send a post in the TurboGears mailing list about why
identity does not play nice with assign_mapper we would surely find a
solution to your issue :)

I used a lot of assign_mapper myself with TG a few months back, I now
switched to mapper in order to become 0.4 compliant, but I can tell
you TG 1.0.2/1.0.3.x are assign_mapper friendly... 1.0.4beta1 should
also be usable with assign_mapper.

Regards,
Florent.

James Brady

unread,
Oct 16, 2007, 8:44:12 AM10/16/07
to sqlalchemy
Hi Florent,

On Oct 11, 1:01 pm, "Florent Aide" <florent.a...@gmail.com> wrote:


> On 10/11/07, James Brady <james.colin.br...@gmail.com> wrote:
>
> > > Did you call assign_mapper on Ownership?
>
> [...]
>
> > However, I'm using the identity framework (part of TurboGears) which
> > unfortunately doesn't play nicely with assign_mapper... shame!
>
> If you use SA 0.3.10 I would advise you to use mapper (from
> sqlalchemy.orm import mapper) instead of assign_mapper.
> If you could send a post in the TurboGears mailing list about why
> identity does not play nice with assign_mapper we would surely find a
> solution to your issue :)

So, using mapper rather than assign_mapper brings me back to my
original problem of not being able to use the relations I've set up in
my models - the ResultProxy object only has the basic keys from the
table definition.

> I used a lot of assign_mapper myself with TG a few months back, I now
> switched to mapper in order to become 0.4 compliant, but I can tell
> you TG 1.0.2/1.0.3.x are assign_mapper friendly... 1.0.4beta1 should
> also be usable with assign_mapper.

So is assign_mapper deprecated in SA 0.4? Is sqlalchemy.orm.mapper the
direction I should be heading in?

Thanks,
James

Reply all
Reply to author
Forward
0 new messages