Re: [sqlalchemy] referencing a `label` in a group by clause ?

2014-07-23 Thread Jonathan Vanasco
On Wednesday, July 23, 2014 7:50:48 PM UTC-4, Michael Bayer wrote: > > you should be able to say: > > group_by(“first_letter”) > > should go right in under that label. > > works for order by at least. > i can't believe i didn't try that first. works as promised. thanks! -- You received th

Re: [sqlalchemy] referencing a `label` in a group by clause ?

2014-07-23 Thread Michael Bayer
you should be able to say: group_by("first_letter") should go right in under that label. works for order by at least. On Jul 23, 2014, at 7:31 PM, Jonathan Vanasco wrote: > I want to generate this SQL: > > SELECT > SUBSTR(domain_name, 1, 1) as first_letter, >

[sqlalchemy] referencing a `label` in a group by clause ?

2014-07-23 Thread Jonathan Vanasco
I want to generate this SQL: SELECT SUBSTR(domain_name, 1, 1) as first_letter, COUNT(domain_name) as counted FROM domain GROUP BY first_letter; The closest I can get to it is this: dbSession.query( sqlalchemy.func.count( model

Re: [sqlalchemy] handling Integrity error gracefully

2014-07-23 Thread Michael Bayer
On Jul 23, 2014, at 4:52 PM, Milind Vaidya wrote: > Well only caveat here is that I am inserting a list of objects in one go > using session.add_all() if you do an add_all(), then say flush(), it will emit INSERT statements for all of them. If any INSERT fails, the whole operation is rolle

Re: [sqlalchemy] handling Integrity error gracefully

2014-07-23 Thread Milind Vaidya
Well only caveat here is that I am inserting a list of objects in one go using session.add_all() On Wednesday, July 23, 2014 2:01:18 PM UTC-5, Michael Bayer wrote: > > use a savepoint: > > from sqlalchemy import exc > try: >with session.begin_nested(): > session.add(MyObject()) >

Re: [sqlalchemy] Strange issue with unicode

2014-07-23 Thread Gunnlaugur Thor Briem
I've posted the answer on StackOverflow. You have client_encoding = sql_ascii in your postgresql.conf, and you just need to change that to utf8 or override it in your create_engine call. Cheers, Gulli On Wed, Jul 23, 2014 at 6:59 PM, Michael Bayer wrote: > > On Jul 23, 2014, at 1:20 PM, Andr

Re: [sqlalchemy] handling Integrity error gracefully

2014-07-23 Thread Michael Bayer
use a savepoint: from sqlalchemy import exc try: with session.begin_nested(): session.add(MyObject()) session.flush() except exc.IntegrityError: pass session.commit() personally I prefer just to emit a SELECT first, which may be a per-row SELECT or may be a prefetch of al

[sqlalchemy] Re: handling Integrity error gracefully

2014-07-23 Thread Jonathan Vanasco
If this happens rarely and your database supports savepoints, you can just do the insert within one. if you catch an integrity error, do a rollback and then select. `begin_nested` is the SqlAlchemy command to handle savepoints. -- You received this message because you are subscribed to the Go

Re: [sqlalchemy] Strange issue with unicode

2014-07-23 Thread Michael Bayer
On Jul 23, 2014, at 1:20 PM, Andrew Pashkin wrote: > Hi all! > > I have an issue with unicode and SQLAlchemy. > > I've created topic on SO: > http://stackoverflow.com/questions/24795444/how-to-save-unicode-with-sqlalchemy > > And repository with Vagrant/Ansible setup, so you can easily reprod

[sqlalchemy] handling Integrity error gracefully

2014-07-23 Thread Milind Vaidya
I have a script which updates DB periodically. There are some base table which may undergo some change but very rarely. In such case if this script tries to insert the already present value it will throw integrity error. Is there any way of dealing with this other that first fetch the value man

[sqlalchemy] Strange issue with unicode

2014-07-23 Thread Andrew Pashkin
Hi all! I have an issue with unicode and SQLAlchemy. I've created topic on SO: http://stackoverflow.com/questions/24795444/how-to-save-unicode-with-sqlalchemy And repository with Vagrant/Ansible setup, so you can easily reproduce this bug on your local machine: https://github.com/AndrewPashkin/

Re: [sqlalchemy] Multiple tables and foreignkey constraints

2014-07-23 Thread Jonathan Vanasco
I would probably do: # Keep track of manufacturer names t_manufacturer = Table(u'manufacturer', metadata, Column(u'id', Integer, primary_key=True, Column(u'name', String(20)) ) # Keep track of model names t_model = Table(u'model', metadata, Column(u'id', Integer, primary_key=True, Column(u'manufa