[BangPypers] SQLAlchemy and 'non-trivial' default values for a column

Kiran Jonnalagadda jace at pobox.com
Mon Feb 18 09:59:47 CET 2013


Sriram, I just noticed this thread. What you are seeking is fairly trivial once you figure out SQLAlchemy a little better.

Here is how I implemented container-unique incrementing ids:

https://github.com/hasgeek/coaster/blob/master/coaster/sqlalchemy.py#L197

The trick is to override __init__ and set the value of the column to an SQL expression that is evaluated database-side on commit. In my case, I'm checking for the max value from existing instances that share the same parent. In your case, it'll be instances that share the same date.

Kiran 

-- 
Kiran Jonnalagadda
http://jace.zaiki.in/
http://hasgeek.com/


On Tuesday, 12 February 2013 at 2:02 PM, Sriram Karra wrote:

> On Tue, Feb 12, 2013 at 12:32 PM, Dhruv Baldawa <dhruvbaldawa at gmail.com (mailto:dhruvbaldawa at gmail.com)>wrote:
> 
> > Then I guess you are doing it the wrong way. You should only have a single
> > session object (unless working with multiple databases simultaneously). The
> > object should be binded with whatever connection is required.
> > 
> 
> 
> 
> 
> I do have two separate databases - a demo database for training purposes
> and a live production database. Both have to be functional and available
> simultaneously.
> 
> Anyway, thanks for your help. I was only trying to figure out some way of
> achieving what I wanted without accessing the session object. I guess
> that's not possible. Good to know that.
> 
> Cheers,
> Karra
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org (mailto:BangPypers at python.org)
> http://mail.python.org/mailman/listinfo/bangpypers
> 
> 




More information about the BangPypers mailing list