Rearrange columns in SQLAlchemy core Select object

78 views
Skip to first unread message

Matthew Rocklin

unread,
Sep 19, 2014, 6:30:14 AM9/19/14
to sqlal...@googlegroups.com

How can I reorder the columns in a SQLAlchemy query object without causing undue nesting?

I've asked this question with an example on StackOverflow.  Thought I'd advertise it here as well.  Please let me know if this is not preferred.


http://stackoverflow.com/questions/25914329/rearrange-columns-in-sqlalchemy-select-object


Best,

-Matthew

Michael Bayer

unread,
Sep 19, 2014, 9:49:18 AM9/19/14
to sqlal...@googlegroups.com
you use with_only_columns but the columns you place into it must come from that list that was sent to the select() originally, and *not* the exported columns of the select itself.

You either have to hold onto these columns externally, or get at them via the select.inner_columns accessor.



--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Matthew Rocklin

unread,
Sep 19, 2014, 9:51:02 AM9/19/14
to sqlal...@googlegroups.com
Inner_columns ended up being the solution on stack-overflow.  The current answer that provides this is somewhat convoluted though.  

If you wanted to say exactly what you just said on SO I'd be happy to mark it as correct for future reference.

--
You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/OxowS9BhAKE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com.

Michael Bayer

unread,
Sep 19, 2014, 10:21:05 AM9/19/14
to sqlal...@googlegroups.com
its at the bottom but i didn’t go through the effort to make an example….

Matthew Rocklin

unread,
Sep 22, 2014, 1:26:26 PM9/22/14
to sqlal...@googlegroups.com
Thanks for the response Michael.  

If you're interested, a follow-up question.  

Michael Bayer

unread,
Sep 22, 2014, 1:31:04 PM9/22/14
to sqlal...@googlegroups.com
there’s no magic on that one, you’d need to probably write some routine that digs into each select() and does what you need, looking at stmt._whereclause and whatever else you want to pull from each one and then build up a new select() that does what you want.  The introspection of a Select object is semi-public at this point but basic things like where/order_by etc. are directly available if you take a peek at the source.

Matthew Rocklin

unread,
Sep 22, 2014, 1:32:58 PM9/22/14
to sqlal...@googlegroups.com
My current solution is to rely on `replace_selectable` but it's not particularly robust.

Michael Bayer

unread,
Sep 22, 2014, 1:35:50 PM9/22/14
to sqlal...@googlegroups.com
this is probably already apparent but if I had this sort of problem, I’d more be asking why do I have this problem in the first place, that is, my program has made these two SELECT objects that need to be combined, they instead should be making two “data criteria” objects of some kind that can be constructed into a statement later.   that is, you need more abstraction here and you need to bind to the SQL expression system later.

Matthew Rocklin

unread,
Sep 22, 2014, 1:46:22 PM9/22/14
to sqlal...@googlegroups.com
Hrm, that's an interesting thought.  Any interest in a real-time conversation?

This work is for Blaze btw.  I'm lowering a relational algebra abstraction to a variety of other systems (pandas, spark, python, sqlalchemy, etc...)
Reply all
Reply to author
Forward
0 new messages