[sqlalchemy] unbound method execute() must be called with Session instance as first argument (got Select instance instead)
Hi all! Why I get unbound method execute() must be called with Session instance as first argument (got Select instance instead) with following code? from twisted.internet import reactor, task, threads from twisted.application import service from twisted.python import log from sqlalchemy import orm, create_engine from sqlalchemy.sql import select from eps.model import offices_table url = 'postgres://test:t...@127.0.0.1:5432/eps' def create_session(): return orm.sessionmaker(bind=create_engine(url), expire_on_commit=False) def require_session(f): def wrapper(*args, **kwargs): s = create_session() try: return f(session=s, *args, **kwargs) except Exception, e: log.err(e) s.rollback() raise finally: s.close() return wrapper @require_session def _getTimers(session=None): return session.execute(select ([offices_table.c.dayopen_time]).distinct(). order_by (offices_table.c.dayopen_time)).fetchall() def getTimers(): return threads.deferToThread(_getTimers) log.msg('Timers: %r' % getTimers()) application = service.Application('Timmer') --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: unbound method execute() must be called with Session instance as first argument (got Select instance instead)
sector119 wrote: Hi all! Why I get unbound method execute() must be called with Session instance as first argument (got Select instance instead) with following code? from twisted.internet import reactor, task, threads from twisted.application import service from twisted.python import log from sqlalchemy import orm, create_engine from sqlalchemy.sql import select from eps.model import offices_table url = 'postgres://test:t...@127.0.0.1:5432/eps' def create_session(): return orm.sessionmaker(bind=create_engine(url), expire_on_commit=False) should be create_session = orm.sessionmaker(...) def require_session(f): def wrapper(*args, **kwargs): s = create_session() try: return f(session=s, *args, **kwargs) except Exception, e: log.err(e) s.rollback() raise finally: s.close() return wrapper @require_session def _getTimers(session=None): return session.execute(select ([offices_table.c.dayopen_time]).distinct(). order_by (offices_table.c.dayopen_time)).fetchall() def getTimers(): return threads.deferToThread(_getTimers) log.msg('Timers: %r' % getTimers()) application = service.Application('Timmer') --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Adding new columns to a table
Hello, my question is quite a basic one, but I couldn't find an answer, maybe because it's too basic. I have a table User and want to add a new Column('donated', Boolean). Then I initialize the database with metadata.create_all (self.database_engine) but in phpPgAdmin there are no changes. Also, if I modify an existing column, eg. from Column('subject', String (128)) to Column('subject', String(128), default='(no subject)'), and then do create_all, nothing changes. Is create_all the wrong thing to do there, or where am I going wrong? Regards, Tom --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Reentrant data population script using sql alchemy
gizli wrote: Thanks a lot Michael. The merge functionality is what I was looking for. This also exposed a flaw in our model I think. We keep using the auto-increment primary keys which would make merge() impossible to use in the scenario I was describing. Right? if you want to merge data over existing data then you'd have to know the primary keys ahead of time, yes. that doesn't mean you can't use autoincrement keys but the typical scenario of import data from a file repeatedly probably includes a deterministic primary key setup, sure. The easiest way to do auto insert or update given an object that you've loaded from a file is to use merge(): myobj = session.merge(myobj) which will do the SELECT for you. If the object is already loaded, no SELECT is emitted. So it follows then that you can SELECT all the rows of the table ahead of time, keep them referenced in a collection: all_objects = session.query(MyStuff).all() then you can merge() each element you pull from the file as needed, and no additional SQL should be emitted to fetch any of it. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Reentrant data population script using sql alchemy
gizli wrote: Thanks a lot Michael. The merge functionality is what I was looking for. This also exposed a flaw in our model I think. We keep using the auto-increment primary keys which would make merge() impossible to use in the scenario I was describing. Right? autoincrement or not has no bearing upon the operation of merge(). The easiest way to do auto insert or update given an object that you've loaded from a file is to use merge(): myobj = session.merge(myobj) which will do the SELECT for you. If the object is already loaded, no SELECT is emitted. So it follows then that you can SELECT all the rows of the table ahead of time, keep them referenced in a collection: all_objects = session.query(MyStuff).all() then you can merge() each element you pull from the file as needed, and no additional SQL should be emitted to fetch any of it. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Updating database after after_insert()
I don't know why but now exception is: InvalidRequestError: Instance 'DBAccount at 0x134360d0' is not persisted On 11 Sie, 01:34, grassoalvaro grassoalv...@yahoo.com wrote: Hi, example from Pylons application, but i think that framework doesn't matter here: class DBAccountExtension(orm.interfaces.MapperExtension): def after_insert(self, mapper, connection, instance): response = hrd.user_create(instance.to_hrd()) if response['status'] == hrd.STATUS_OK: instance.csa = response['resdata']['user']['id'] db.save_or_update(instance) db.commit() return orm.interfaces.EXT_CONTINUE After saving model i'm making request to some API (to save data there also). API return status information (response['resdata']['user'] ['id']) which i want to update in database so i'm trying to do db.save_or_update(instance) and then db.commit() but after this 2 lines application prints exception: InvalidRequestError: The transaction is closed Any solutions? (sorry for my english). --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Updating database after after_insert()
you can't do Session operations and commits inside of after_insert(). you're already in the session.flush() operation. I guess I have to make this clearer for every individual method in the docs. grassoalvaro wrote: I don't know why but now exception is: InvalidRequestError: Instance 'DBAccount at 0x134360d0' is not persisted On 11 Sie, 01:34, grassoalvaro grassoalv...@yahoo.com wrote: Hi, example from Pylons application, but i think that framework doesn't matter here: class DBAccountExtension(orm.interfaces.MapperExtension): def after_insert(self, mapper, connection, instance): response = hrd.user_create(instance.to_hrd()) if response['status'] == hrd.STATUS_OK: instance.csa = response['resdata']['user']['id'] db.save_or_update(instance) db.commit() return orm.interfaces.EXT_CONTINUE After saving model i'm making request to some API (to save data there also). API return status information (response['resdata']['user'] ['id']) which i want to update in database so i'm trying to do db.save_or_update(instance) and then db.commit() but after this 2 lines application prints exception: InvalidRequestError: The transaction is closed Any solutions? (sorry for my english). --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Updating database after after_insert()
Hmm, but after_insert() is the best place where i can put this king logic (updating/inserting data in may places). So i can't use session operations inside of after_insert() at all? On 11 Sie, 18:35, Michael Bayer mike...@zzzcomputing.com wrote: you can't do Session operations and commits inside of after_insert(). you're already in the session.flush() operation. I guess I have to make this clearer for every individual method in the docs. grassoalvaro wrote: I don't know why but now exception is: InvalidRequestError: Instance 'DBAccount at 0x134360d0' is not persisted On 11 Sie, 01:34, grassoalvaro grassoalv...@yahoo.com wrote: Hi, example from Pylons application, but i think that framework doesn't matter here: class DBAccountExtension(orm.interfaces.MapperExtension): def after_insert(self, mapper, connection, instance): response = hrd.user_create(instance.to_hrd()) if response['status'] == hrd.STATUS_OK: instance.csa = response['resdata']['user']['id'] db.save_or_update(instance) db.commit() return orm.interfaces.EXT_CONTINUE After saving model i'm making request to some API (to save data there also). API return status information (response['resdata']['user'] ['id']) which i want to update in database so i'm trying to do db.save_or_update(instance) and then db.commit() but after this 2 lines application prints exception: InvalidRequestError: The transaction is closed Any solutions? (sorry for my english). --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Updating database after after_insert()
This is not exactly what i was looking for. I want to connect only one model with some kind of operations, not all session... On 11 Sie, 19:36, Michael Bayer mike...@zzzcomputing.com wrote: grassoalvaro wrote: Hmm, but after_insert() is the best place where i can put this king logic (updating/inserting data in may places). So i can't use session operations inside of after_insert() at all? nope. use before_flush() or after_flush() on SessionExtension for that. On 11 Sie, 18:35, Michael Bayer mike...@zzzcomputing.com wrote: you can't do Session operations and commits inside of after_insert(). you're already in the session.flush() operation. I guess I have to make this clearer for every individual method in the docs. grassoalvaro wrote: I don't know why but now exception is: InvalidRequestError: Instance 'DBAccount at 0x134360d0' is not persisted On 11 Sie, 01:34, grassoalvaro grassoalv...@yahoo.com wrote: Hi, example from Pylons application, but i think that framework doesn't matter here: class DBAccountExtension(orm.interfaces.MapperExtension): def after_insert(self, mapper, connection, instance): response = hrd.user_create(instance.to_hrd()) if response['status'] == hrd.STATUS_OK: instance.csa = response['resdata']['user']['id'] db.save_or_update(instance) db.commit() return orm.interfaces.EXT_CONTINUE After saving model i'm making request to some API (to save data there also). API return status information (response['resdata']['user'] ['id']) which i want to update in database so i'm trying to do db.save_or_update(instance) and then db.commit() but after this 2 lines application prints exception: InvalidRequestError: The transaction is closed Any solutions? (sorry for my english). --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Updating database after after_insert()
grassoalvaro wrote: This is not exactly what i was looking for. I want to connect only one model with some kind of operations, not all session... scan through Session.dirty etc. for the objects you want. if a new extension MapperExtension.before_flush() were added it would be working more or less the same way anyway. On 11 Sie, 19:36, Michael Bayer mike...@zzzcomputing.com wrote: grassoalvaro wrote: Hmm, but after_insert() is the best place where i can put this king logic (updating/inserting data in may places). So i can't use session operations inside of after_insert() at all? nope. use before_flush() or after_flush() on SessionExtension for that. On 11 Sie, 18:35, Michael Bayer mike...@zzzcomputing.com wrote: you can't do Session operations and commits inside of after_insert(). you're already in the session.flush() operation. I guess I have to make this clearer for every individual method in the docs. grassoalvaro wrote: I don't know why but now exception is: InvalidRequestError: Instance 'DBAccount at 0x134360d0' is not persisted On 11 Sie, 01:34, grassoalvaro grassoalv...@yahoo.com wrote: Hi, example from Pylons application, but i think that framework doesn't matter here: class DBAccountExtension(orm.interfaces.MapperExtension): def after_insert(self, mapper, connection, instance): response = hrd.user_create(instance.to_hrd()) if response['status'] == hrd.STATUS_OK: instance.csa = response['resdata']['user']['id'] db.save_or_update(instance) db.commit() return orm.interfaces.EXT_CONTINUE After saving model i'm making request to some API (to save data there also). API return status information (response['resdata']['user'] ['id']) which i want to update in database so i'm trying to do db.save_or_update(instance) and then db.commit() but after this 2 lines application prints exception: InvalidRequestError: The transaction is closed Any solutions? (sorry for my english). --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Updating database after after_insert()
Oh, that is a good idea and it's working. Thanks ;-) On 11 Sie, 20:12, Michael Bayer mike...@zzzcomputing.com wrote: grassoalvaro wrote: This is not exactly what i was looking for. I want to connect only one model with some kind of operations, not all session... scan through Session.dirty etc. for the objects you want. if a new extension MapperExtension.before_flush() were added it would be working more or less the same way anyway. On 11 Sie, 19:36, Michael Bayer mike...@zzzcomputing.com wrote: grassoalvaro wrote: Hmm, but after_insert() is the best place where i can put this king logic (updating/inserting data in may places). So i can't use session operations inside of after_insert() at all? nope. use before_flush() or after_flush() on SessionExtension for that. On 11 Sie, 18:35, Michael Bayer mike...@zzzcomputing.com wrote: you can't do Session operations and commits inside of after_insert(). you're already in the session.flush() operation. I guess I have to make this clearer for every individual method in the docs. grassoalvaro wrote: I don't know why but now exception is: InvalidRequestError: Instance 'DBAccount at 0x134360d0' is not persisted On 11 Sie, 01:34, grassoalvaro grassoalv...@yahoo.com wrote: Hi, example from Pylons application, but i think that framework doesn't matter here: class DBAccountExtension(orm.interfaces.MapperExtension): def after_insert(self, mapper, connection, instance): response = hrd.user_create(instance.to_hrd()) if response['status'] == hrd.STATUS_OK: instance.csa = response['resdata']['user']['id'] db.save_or_update(instance) db.commit() return orm.interfaces.EXT_CONTINUE After saving model i'm making request to some API (to save data there also). API return status information (response['resdata']['user'] ['id']) which i want to update in database so i'm trying to do db.save_or_update(instance) and then db.commit() but after this 2 lines application prints exception: InvalidRequestError: The transaction is closed Any solutions? (sorry for my english). --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Adding method to ORM mapped object
From what I see, you forgot the self argument in the getRendered function definition. def getRendered(self): return '%s\n%s' % (self.title, self.content) On Aug 10, 5:00 am, Andreas andr...@flausch.at wrote: Arghh... I got it. The problem was that I wasn't querying through a session, but doing a selection without one, so the object wasn't bound to the class ... Some old code fragment. Quite as you said. Thanks for your help :) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] how sum elements with filters
Hi sqlalchemy, I'm a newbie to SA. I hope you could help me with the following problem. Say that I have: class Position(Base): __tablename__ = 'Position' id = Column('id', Integer, primary_key=True) position = Column('name', String) #center, guard etc' class Player(Base): __tablename__ = 'Player' id = Column('id', Integer, primary_key=True) name = Column('name', String) team = Column('team', String) position_id = Column('position_id', Integer, ForeignKey('Position.id')) position = relation('Position') class Stats(Base): __tablename__ = 'Stats' id = Column('id', Integer, primary_key=True) name = Column('name', String) points = Column('points', Integer) Player_id = Column('player_id', Integer, ForeignKey('Player.id')) league = relation('Player') How can I retrieve the sum of points for each of the player positions? In other words: Retrieving the sum of points of all of the guards, centers, etc'. How can this query be further limited to a specific team (simple join+filter, right?) Thanks, Doron. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Adding new columns to a table
1. append_column() only raises that error if the Column() you're sticking there was already associated with some other table or selectable object. 2. you need ALTER TABLE. you can of course just say engine.execute(ALTER TABLE rest of statement). For more options, there is an FAQ entry here: http://www.sqlalchemy.org/trac/wiki/FAQ#DoesSQLAlchemysupportALTERTABLECREATEVIEWCREATETRIGGERSchemaUpgradeFunctionality Nelson wrote: I have virtually the same question. To put it another way I have an ORM mapped Table() in my Python code. There is a Column() in that table that is not yet in my Postgres database. How do I get that Column () into the database? I have tried table.append_column(col) but it's telling me this Column already has a table! I'm using 0.5.3 and am about to go to 0.5.5 Thanks in advance for any help. Nelson On Aug 11, 8:28 am, tom t.mis...@gmail.com wrote: Hello, my question is quite a basic one, but I couldn't find an answer, maybe because it's too basic. I have a table User and want to add a new Column('donated', Boolean). Then I initialize the database with metadata.create_all (self.database_engine) but in phpPgAdmin there are no changes. Also, if I modify an existing column, eg. from Column('subject', String (128)) to Column('subject', String(128), default='(no subject)'), and then do create_all, nothing changes. Is create_all the wrong thing to do there, or where am I going wrong? Regards, Tom --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Proper way to use case() in a filter()?
I have a case(...) that looks right when I print it and am now trying to use it in a filter, something like: sess.query(User).select_from(join(...)).filter(case(...) == 1) But I get an error. When dissecting the parts, the problem is coming from the case(...) == 1 part. Can someone tell me how to do this properly? Thanks. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Adding new columns to a table
I have virtually the same question. To put it another way I have an ORM mapped Table() in my Python code. There is a Column() in that table that is not yet in my Postgres database. How do I get that Column () into the database? I have tried table.append_column(col) but it's telling me this Column already has a table! I'm using 0.5.3 and am about to go to 0.5.5 Thanks in advance for any help. Nelson On Aug 11, 8:28 am, tom t.mis...@gmail.com wrote: Hello, my question is quite a basic one, but I couldn't find an answer, maybe because it's too basic. I have a table User and want to add a new Column('donated', Boolean). Then I initialize the database with metadata.create_all (self.database_engine) but in phpPgAdmin there are no changes. Also, if I modify an existing column, eg. from Column('subject', String (128)) to Column('subject', String(128), default='(no subject)'), and then do create_all, nothing changes. Is create_all the wrong thing to do there, or where am I going wrong? Regards, Tom --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Proper way to use case() in a filter()?
Not much detail to tell what is wrong. Here is a contrived but working example based on one of my examples: ut = Table('user',meta, Column('uid',String,primary_key=True), Column('name',String) ) kt = Table('keywords',meta, Column('keyword',String,primary_key=True) ) ukt = Table('userkeywords',meta, Column('uid',String,ForeignKey('user.uid')), Column('kw',String,ForeignKey('keywords.keyword')) ) class User(Base): __table__ = ut keywords = relation('Keyword', secondary=ukt, backref='users') def __repr__(s): return User %s:%s % (s.uid,s.name) class Keyword(Base): __table__ = kt def __repr__(s): return Keyword %s % s.keyword q = session.query(User).\ select_from(join(ut,ukt,ut.c.uid==ukt.c.uid)).\ filter(case([(ut.c.uid=='mike','M'), (ut.c.uid=='sue','F')], else_='X') == 'M') -- Mike Conley --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---