Question

Lets say you have a simple SQLAlchemy mapped object like this:

class User(Base):
    __tablename__ = 'users'
    id = sqlalchemy.Column(sqlalchemy.Integer, autoincrement=True,primary_key=True)
    name = sqlalchemy.Column(sqlalchemy.Unicode(255), unique=True, nullable=False)
    email = sqlalchemy.Column(sqlalchemy.Unicode(255), nullable=False)
    address = sqlalchemy.Column(sqlalchemy.Unicode(255), nullable=False)

Users are created:

sess.add(User(name=u'One', email=u'one@example.com', address='An address'))
sess.add(User(name=u'Two', email=u'two@example.com', address='An address'))
sess.add(User(name=u'Three', email=u'three@example.com', address='An address'))

Than you can query these three users with:

1.)

sess.query(User).all()
[<__main__.User object at 0x2fb9790>, <__main__.User object at 0x2fb9810>, <__main__.User object at 0x2fb9890>]

Which gives you a list of User objects.

2.)

sess.query(User.id, User.name, User.email, User.address).all()
[(1, u'one@example.com', u'An address'), (2, u'two@example.com', u'An address'), (3, u'three@example.com', u'An address')]

Which gives you a list of KeyedTuple objects where you can refer fields by attribute name like sess.query(User.id, User.name, User.email, User.address).all()[0].name

3.)

sess.execute(sess.query(User)).fetchall()
[(1, u'One', u'one@example.com', u'An address'), (2, u'Two', u'two@example.com', u'An address'), (3, u'Three', u'three@example.com', u'An address')]

Which gives you a list of RowProxy objects, where you can refer to fields like sess.execute(sess.query(User)).fetchall()[0]['users_name'].

My question would be if there is an easy way to get the result of the second query example without having to list all the attributes of the user class or if there is a way to get the results of the third query with keys like 'name' instead of 'users_name', so the same as the attribute name in the User class.

So basically what I would need is an object that contains all the column attributes of the User class and the attributes can be referred like res.name or res['name']. So I only need the attributes but not the whole User object with all the other stuff.

This would make an iteration though the attributes very easy because you could say res.keys() for example.

I hope my question is clear. I see that this is very similar to a User instance, but still not the same.

The preferred solution would be the KeyedTuple as a result without having to explicitly list the attributes.

Thanks in advance.

Was it helpful?

Solution

I am not exactly sure what your goal is, but are you perhaps trying to deal with dynamic column data? Other ways may exist, but you can always unpack the table columns as query arguments like this:

>>> user_columns = User.__table__.columns
>>> sess.query(*user_columns).all()  # unpack user_columns into arguments
[(1, u'One', u'one@example.com', u'An address'), (2, u'Two', u'two@example.com', u'An address'), (3, u'Three', u'three@example.com', u'An address')]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top