Order of properties from inspection

26 views
Skip to first unread message

Denis Rykov

unread,
May 9, 2016, 8:11:21 AM5/9/16
to sqlalchemy
Hello.

Why "address_" key on first place of list, is it expected behaviour?

>>> from sqlalchemy.ext.declarative import declarative_base
>>> from sqlalchemy.inspection import inspect
>>> from sqlalchemy import Integer, Column, Unicode
>>> 
>>> Base = declarative_base()
>>> 
>>> 
>>> class Venue(Base):
...     __tablename__ = 'venue'
...     id = Column(Integer, primary_key=True)
...     name = Column(Unicode, nullable=False)
...     address_ = Column('address', Unicode, nullable=False)
... 
>>> inspect(Venue).columns.keys()
['address_', 'id', 'name']


Mike Bayer

unread,
May 9, 2016, 9:31:31 AM5/9/16
to sqlal...@googlegroups.com
not a bad question. Those attributes are the mapper-assigned attributes
and I'd guess that because address_ has an alternate name, declarative
is setting it up in a different step. The attributes on a class can't
be ordered in any case.

To get the real order of columns at the Core level use the table:

Venue.__table__.columns.keys()
> --
> 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
> <mailto:sqlalchemy+...@googlegroups.com>.
> To post to this group, send email to sqlal...@googlegroups.com
> <mailto:sqlal...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

Denis Rykov

unread,
May 9, 2016, 9:50:39 AM5/9/16
to sqlal...@googlegroups.com
Thanks for quick response. I've tried your approach:

>>> Venue.__table__.columns.keys()
['id', 'name', 'address']

But how I can get class attributes in the same order as real table columns?


--
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/k0MrlMBvAls/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.

Mike Bayer

unread,
May 9, 2016, 10:01:28 AM5/9/16
to sqlal...@googlegroups.com

ideally declarative would try to preserve this ordering for straight
columns, for now you can do it like this:


[c.key for c in sorted(inspect(Venue).columns, key=lambda col:
col._creation_order)]
> <mailto:sqlalchemy%2Bunsu...@googlegroups.com>
> <mailto:sqlalchemy+...@googlegroups.com
> <mailto:sqlalchemy%2Bunsu...@googlegroups.com>>.
> To post to this group, send email to sqlal...@googlegroups.com
> <mailto:sqlal...@googlegroups.com>
> <mailto:sqlal...@googlegroups.com
> <mailto:sqlal...@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 a topic in
> the Google Groups "sqlalchemy" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/sqlalchemy/k0MrlMBvAls/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> sqlalchemy+...@googlegroups.com
> <mailto:sqlalchemy%2Bunsu...@googlegroups.com>.

Denis Rykov

unread,
May 9, 2016, 12:41:57 PM5/9/16
to sqlal...@googlegroups.com
It not returning expected result:

>>> [c.key for c in sorted(inspect(Venue).columns, key=lambda col: col._creation_order)] 
['id', 'name', 'address']

I want to get ['id', 'name', 'address_'].


To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.

Mike Bayer

unread,
May 9, 2016, 12:47:39 PM5/9/16
to sqlal...@googlegroups.com

OK let's make it take both into account....


print [c.key for c in sorted(inspect(Venue).column_attrs, key=lambda
col: col.columns[0]._creation_order)]



On 05/09/2016 12:41 PM, Denis Rykov wrote:
> It not returning expected result:
>
> >>> [c.key for c in sorted(inspect(Venue).columns, key=lambda col:
> col._creation_order)]
> ['id', 'name', 'address']
>
> I want to get ['id', 'name', 'address_'].
>
>
> On Mon, May 9, 2016 at 9:01 PM, Mike Bayer <mik...@zzzcomputing.com
> <mailto:mik...@zzzcomputing.com>> wrote:
>
>
> ideally declarative would try to preserve this ordering for straight
> columns, for now you can do it like this:
>
>
> [c.key for c in sorted(inspect(Venue).columns, key=lambda col:
> col._creation_order)]
>
>
>
> On 05/09/2016 09:50 AM, Denis Rykov wrote:
>
> Thanks for quick response. I've tried your approach:
>
> Venue.__table__.columns.keys()
>
> ['id', 'name', 'address']
>
> But how I can get class attributes in the same order as real
> table columns?
>
> On Mon, May 9, 2016 at 8:31 PM, Mike Bayer
> <mik...@zzzcomputing.com <mailto:mik...@zzzcomputing.com>
> <mailto:mik...@zzzcomputing.com
> <mailto:sqlalchemy%2Bunsu...@googlegroups.com
> <mailto:sqlalchemy%252Buns...@googlegroups.com>>
> <mailto:sqlalchemy+...@googlegroups.com
> <mailto:sqlalchemy%2Bunsu...@googlegroups.com>
> <mailto:sqlalchemy%2Bunsu...@googlegroups.com
> <mailto:sqlalchemy%252Buns...@googlegroups.com>>>.
> To post to this group, send email to
> sqlal...@googlegroups.com <mailto:sqlal...@googlegroups.com>
> <mailto:sqlal...@googlegroups.com
> <mailto:sqlal...@googlegroups.com>>
> <mailto:sqlal...@googlegroups.com
> <mailto:sqlal...@googlegroups.com>
> <mailto:sqlal...@googlegroups.com
> <mailto:sqlal...@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 a
> topic in
> the Google Groups "sqlalchemy" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/sqlalchemy/k0MrlMBvAls/unsubscribe.
> To unsubscribe from this group and all its topics, send an
> email to
> sqlalchemy+...@googlegroups.com
> <mailto:sqlalchemy%2Bunsu...@googlegroups.com>
> <mailto:sqlalchemy%2Bunsu...@googlegroups.com
> <mailto:sqlalchemy%252Buns...@googlegroups.com>>.

Denis Rykov

unread,
May 9, 2016, 12:53:28 PM5/9/16
to sqlal...@googlegroups.com
Thanks!

To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages