Hello everybody!

What I want sometimes is to query some columns but to map the resulting 
rows into custom data classes rather than tuples, like with 
values(*columns), or SQLA entities.
I found that Bundles 
(https://docs.sqlalchemy.org/en/latest/orm/loading_columns.html#bundles) 
could potentially help, but what I see there is that I can process rows 
with create_row_processor and give the immediate result of one-to-one 
converting every single row.
Let's say I have User and Address with 1:N relation:


class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    name = Column(String)

    addresses = relationship('Address')


class Address(Base):
    __tablename__ = 'addresses'

    id = Column(Integer, primary_key=True)
    user_id = Column(ForeignKey(User.id))
    city = Column(String)



I'd like to query all users into a custom structure like this:


[{
  'id': 1,
  'name': 'User 1',
  'addresses': [{
    'id': 1,
    'city': 'City 1',
  }, {
    'id': 2,
    'city': 'City 2',
  }],
}, {
  'id': 2,
  'name': 'User 2',
  'addresses': [{
    'id': 3,
    'city': 'City 3',
  }],
}]



It's similar to what ORM does for me when using joinedload for 
relationships, but how to nest related items without ORM identity-mapped 
classes?
Your help would be much appreciated!

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
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.

Reply via email to