On 10/10/2016 01:44 PM, Charles Heizer wrote:
Hello,
I'm not sure if I'm asking this correctly. I'm fairly new to using
SQLAlchemy so I'm sorry if I'm not using the correct terminology.
How do I change the returning column order on a query? Is there an easy
way? I would like to make 'mdate' be the last column.
Thanks!
Example:
*Model*
|
classClient(CommonBase):
__tablename__ ='clients'
id =Column(BigInteger,primary_key=True)
cid
=Column(String(50),nullable=False,index=True,unique=True,info='Client ID')
mdate =Column(DateTime,server_default='1970-01-01
00:00:00',info='Mod Date')
serialno =Column(String(100),server_default='NA',info='Serial
No')
hostname
=Column(String(255),server_default='NA',index=True,info='Host Name')
ipaddr
=Column(String(64),server_default='NA',index=True,info='IP Address')
macaddr =Column(String(64),server_default='NA',info='MAC
Address')
|
*Query*
|
clients =Client.query.all()
so when you query that way, you take all the Column objects you've set
up for Client, throw them into a SELECT, and the results get sent back
into Client objects. It's odd that you would ask for an "order" in that
context; the SQL would render a certain way, but that in no way changes
the results you get back; no matter what the order in the SELECT
statement is, you get Client objects back with no defined ordering of
their attributes.
To get back tuples with a specific ordering, break it out like this:
rows = session.query(Client.id, Client.serialno, Client.mdate)
you get back tuples instead that are ordered in terms of their
attributes. Maybe that's what you're asking.
Otherwise, if you really just want Client objects and the SELECT happens
to put "mdate" last, then make "mdate" the last Column that you
construct when you build up Client:
class Client(...):
__tablename__ = '...'
id = Column(...)
...
mdate = Column(...)
but I can't imagine a scenario in which that makes any difference.
|
--
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+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
--
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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.