I can't help but think that the number of keystrokes required to do a query in SQLAlchemy is a little on the high side.
DBSession.query(User).filter(...).first()
Is there anything wrong with doing something like this?
DBSession = scoped_session(sessionmaker())
class BaseWithDBSession(object):
def delete(self):
DBSession.delete(self)
@classmethod
def query(cls):
return DBSession.query(cls)
Base = declarative_base(cls=BaseWithDBSession)
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
...
user = User.query().filter(User.id==1).one()
user.delete()
This approach seems a lot neater to me, and it also means I don't have to import DBSession between code files. The downside is that all of my classes are tied to a particular session, but I can't think of any situation in which I would want anything different. I'm trying to think of reasons why I haven't seen this approach before (apart from my inexperience)...