confusion on using sqlalchemy with declarative table definitions in pylons, session does not work.

3 views
Skip to first unread message

Krishnakant

unread,
Nov 25, 2009, 1:52:08 PM11/25/09
to pylons-...@googlegroups.com
hello,

I just started to use sqlalchemy in my pylons project.
I liked the declarative syntax of table definition rather than adding
mappers.

But now I am confused.
When I read the official sqlalchemy docs, they say that I don't need to
create the metadata instance because the base instance will have its own
metadata and session.
But in pylons the session and metadata get configured and instantiated
inside the model/meta module.

The following 2 lines of code are the source of my confusion.
from sqlalchemy.ext.declarative import declarative_base
base = declarative_base()

I believe the base instance here will have its own session and metadata
objects?
And how do I access the tables created using this approach?
Should I create the instances using model.class_name.__table__ or is
there another approach?
further more, I see that the Session.commit does not work, naturally
because it is not associated with any table instances using declarative
syntax.
Just as a guess, should the following 2 lines work with some correction?


from sqlalchemy.ext.declarative import declarative_base
base = declarative_base(meta.metadata)

if Yes then how can I create instances of any table inside a control?

I would awaite any guidance on this.
Happy ahcking.
Krishnakant.




Nikolai Drozd

unread,
Nov 25, 2009, 2:00:26 PM11/25/09
to pylons-...@googlegroups.com
Krishnakant wrote:
> hello,
>
> I just started to use sqlalchemy in my pylons project.
> I liked the declarative syntax of table definition rather than adding
> mappers.
>
> [skipped]
>
> from sqlalchemy.ext.declarative import declarative_base
> base = declarative_base(meta.metadata)

That seems to be correct. At least it works in my project :)

>
> if Yes then how can I create instances of any table inside a control?

What do you mean by instance of a table?

Krishnakant

unread,
Nov 25, 2009, 2:17:44 PM11/25/09
to pylons-...@googlegroups.com
On Wed, 2009-11-25 at 21:00 +0200, Nikolai Drozd wrote:
> >
> > from sqlalchemy.ext.declarative import declarative_base
> > base = declarative_base(meta.metadata)
>
> That seems to be correct. At least it works in my project :)
>
Then I believe it should be correct.

> >
> > if Yes then how can I create instances of any table inside a control?
>
> What do you mean by instance of a table?

Ok here is a simple class which I created in my __init__.py file
in /model given the imports I am making specific to declarative_base.

class members(base):
__tablename__ = "person"
id = sa.Column(sa.types.Integer, primary_key = True)
membername = sa.Column(sa.types.String(250))
address = sa.Column(sa.types.String(300))

Let's say in my control I want to add record to this table by getting
values from a request.
Another method in the same control would like to query for all the
members in the table and send the result out as a c.records to be then
rendered into a mako template for form generation using for example
webhelpers.

My question is how can I use the above class to add records and how can
I query the table for records.

The major point of confusion is about the session object.

Where is the actual Session instance? will I have to do some thing like
base.Session or just Session will work?

Same goes for making an instance of a member class for adding records
for example.
should I use the following line?
newMember = model.members.__table__
or is there some thing else I must do?

Happy hacking.
Krishnakant.


Nikolai Drozd

unread,
Nov 25, 2009, 2:22:48 PM11/25/09
to pylons-...@googlegroups.com
Krishnakant wrote:
> On Wed, 2009-11-25 at 21:00 +0200, Nikolai Drozd wrote:
>>> from sqlalchemy.ext.declarative import declarative_base
>>> base = declarative_base(meta.metadata)
>> That seems to be correct. At least it works in my project :)
>>
> Then I believe it should be correct.
>
>>> if Yes then how can I create instances of any table inside a control?
>> What do you mean by instance of a table?
>
> Ok here is a simple class which I created in my __init__.py file
> in /model given the imports I am making specific to declarative_base.
>
> class members(base):
> __tablename__ = "person"
> id = sa.Column(sa.types.Integer, primary_key = True)
> membername = sa.Column(sa.types.String(250))
> address = sa.Column(sa.types.String(300))
>
> Let's say in my control I want to add record to this table by getting
> values from a request.

it may looks like this

from project_name.model.meta import Session

then somewhere in your code

m = members()
m.membername = request.params['membername']
m.address = request.params['address']

Session.add(m)
Session.commit()

> Another method in the same control would like to query for all the
> members in the table and send the result out as a c.records to be then
> rendered into a mako template for form generation using for example
> webhelpers.

to get all records:

Session.query(members).all( )

>
> My question is how can I use the above class to add records and how can
> I query the table for records.

As far as I know all of these are described in documentation pretty well

> The major point of confusion is about the session object.
>
> Where is the actual Session instance? will I have to do some thing like
> base.Session or just Session will work?
>
> Same goes for making an instance of a member class for adding records
> for example.
> should I use the following line?
> newMember = model.members.__table__
> or is there some thing else I must do?
>
> Happy hacking.
> Krishnakant.
>
>
> --
>
> You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
> To post to this group, send email to pylons-...@googlegroups.com.
> To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
>
>

Krishnakant

unread,
Nov 26, 2009, 4:14:02 PM11/26/09
to pylons-...@googlegroups.com
Thanks, it worked for me.
when I passed the metadata instance created by my pylons application.

I think the pylons documentation sure need to be updated because the
information out there is incomplete when it comes to using declarative
syntax for table definition.

Happy hacking.
Krishnakant.


Reply all
Reply to author
Forward
0 new messages