Search code examples
pythonsqlalchemypython-elixir

query.values fails with Elixir


I have a problem with the Elixir declarative layer; I want to retrieve, for every instance of my model, the data of a particular column, like this:

File.query.values("column")

The thing is, it only works with filtered queries, with the simple query of the example it fails with this error :

Could not locate a bind configured on SQL expression or this Session

This kind of looks like a bug in Elixir, but I'm failing to find a workaround, and maybe I totally overlooked something in the way it is supposed to work.


Solution

  • Use the form

    File.query.values(Table.column)
    

    and it should work. It returns a generator, so wrap with list() to get a sequence. See this example interactive output:

    >>> User.query.values('display_name')
    Traceback (most recent call last):
      File "<console>", line 1, in ?
    [snip traceback]
    UnboundExecutionError: Could not locate a bind [ ... ] or this Session
    >>> User.query.values(User.display_name).next()
    (u'Vinay Sajip',)
    

    BTW it's not particularly an Elixir issue - the query returned from an Elixir entity's query property is a standard SQLAlchemy query object. Note the SQLAlchemy documentation for Query.values():

    Return an iterator yielding result tuples corresponding to the given list of columns