3

when I do the following in python using sqlalchemy

someVar = self.session.query(sometable.someVar).group_by(sometable.someVar).all()

I get a list of one variable tuples like

[(var1,), (var2,), ..]

Is there a way of getting a flat list within sqlalchemy. I know how to flatten the list of tuple within python code, but that solution is bit clumsy. I am just wondering if this is possible. My goal is to get a list of distinct entries someVar from sometable.

2
  • 4
    How is following flattening clumsy? someVarFlat = [var[0] for var in someVar]? And No, there is no way to do this in sqlalchemy query directly.
    – van
    Dec 28, 2013 at 18:50
  • 2
    van, I did that. It's not real clumsy, but I was wondering if it were possible to do it within alchemy. Dec 30, 2013 at 16:09

1 Answer 1

3

I use (in @classmethod - that's why cls instance here; I also have added 'session' to class properties=)

list(session.query(cls))

returns exactly the simple list of all DeclarativeBase's instances.

1
  • I think your trouble is because of group_by method. I use pythonic mechanism of filter the sequences with filter() function instead of integrated in the SqlAlchemy one. It's not very efficient, but I do so because of some backward-compatibility reasons. And here it seems to be reasonable approach. Dec 29, 2013 at 11:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.