declarative and session

6 views
Skip to first unread message

karik...@gmail.com

unread,
Dec 23, 2009, 10:20:34 PM12/23/09
to sqlalchemy
I am using declarative style models and very much happy about it.

I see __table__ contains the classic sa.Table reference for advanced
queries. Like that do we have session is attached to the model class
derived from declarative_base?

I am very much curious to make django/rail style save, update, delete
methods on top of sqlalchemy.

I don't know much about Exlir but I see it does things over smart
matching some ruby like stuff. Not sure about community support.

If the session is attached, I can say like

class MyModel(SQLAlchemyDecBase)
...
def save(self):
self.session.add(self)
#or
meta.session.add(self)

Serge Koval

unread,
Dec 27, 2009, 4:19:08 AM12/27/09
to sqlal...@googlegroups.com
You can always do self.session.add(self) in save() without checks. If
your model was already associated with the session before, it won't add
it again.

Here's sample code I use:

def save(self, flush=False):
self.session.add(self)

if flush:
self.session.flush()

def delete(self):
self.session.delete(self)

Serge.

> --
>
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To post to this group, send email to sqlal...@googlegroups.com.
> To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
>
>
>
>

karik...@gmail.com

unread,
Dec 28, 2009, 9:40:04 AM12/28/09
to sqlalchemy
Thank you so much. I will try adding base class derived from
declarative base and implement save, update and delete methods.

Regards,

Krish

On Dec 27, 2:19 pm, Serge Koval <serge.ko...@gmail.com> wrote:
> You can always do self.session.add(self) in save() without checks. If
> your model was already associated with the session before, it won't add
> it again.
>
> Here's sample code I use:
>
> def save(self, flush=False):
>     self.session.add(self)
>
>     if flush:
>         self.session.flush()
>
> def delete(self):
>     self.session.delete(self)
>
> Serge.
>

Reply all
Reply to author
Forward
0 new messages