SQLAlchemy: Getting a single object from joining multiple tables

前端 未结 3 580
说谎
说谎 2021-01-01 01:32

DB - SQLAlchemy setting

Consider a classic setting of two tables - user and api_key, represented by SQLAlchemy objects as:



        
相关标签:
3条回答
  • 2021-01-01 02:13

    Instead of querying objects, query for list of fields instead, in which case SQLAlchemy returns instances of KeyedTuple, which offers KeyedTuple._asdict() method you can use to return arbitrary dictionary:

    def my_function(user_id):
        row =  database.db_session.query(User.name, ApiKey.api_key)\
            .join(ApiKey, User.vioozer_api_key==ApiKey.api_key)\
            .filter(User.user_id==user_id).first()
        return row._asdict()
    
    my_data = my_function('user_00000000000000000000000000000000')
    

    But for your particular query, you do not need even to join on ApiKey as the api_key field is present on the User table:

    row = database.db_session.query(User.name, User.api_key)\
        .filter(User.user_id==user_id).first()
    
    0 讨论(0)
  • 2021-01-01 02:19

    I would add an api_key relationship on User:

    class User(Base):
        __tablename__ = 'user'
        user_id = Column(String)
        user_name = Column(String)
        vioozer_api_key = Column(String, ForeignKey("api_key.api_key"))
        api_key = Relationship('ApiKey', uselist=False)
    

    Then you can do a query like this:

    >>> user = database.db_session.query(User)\
          .filter(User.user_id=='user_00000000000000000000000000000000').first()
    >>> user.api_key
    <ApiKey>
    
    0 讨论(0)
  • 2021-01-01 02:33

    I'm surprised how little discussion there is on this... I need to join two tables and return a single object which has all columns from both tables. I'd expect that is a pretty common usecase - anyways here's the hack I made after hours of looking and failing to find a nice way

    from sqlalchemy import inspect
    def obj_to_dict(obj):
        return {c.key: getattr(obj, c.key) for c in inspect(obj).mapper.column_attrs}
    def flatten_join(tup_list):
        return [{**obj_to_dict(a), **obj_to_dict(b)} for a,b in tup_list]
    

    Here's an example of what it does...

    ########################## SETUP ##########################
    from sqlalchemy import create_engine
    from sqlalchemy.orm import sessionmaker
    from sqlalchemy import Column, Integer, ForeignKey, Text
    from sqlalchemy.ext.declarative import declarative_base
    
    DeclarativeBase = declarative_base()
    
    
    class Base(DeclarativeBase):
        __abstract__ = True
    
        def __repr__(self) -> str:
            items = []
            for key in self.__table__._columns._data.keys():
                val = self.__getattribute__(key)
                items.append(f'{key}={val}')
            key_vals = ' '.join(items)
            name = self.__class__.__name__
            return f"<{name}({key_vals})>"
    
    
    class User(Base):
        __tablename__ = "user"
    
        id = Column(Integer, primary_key=True, index=True)
        username = Column(Text)
    
    
    class Todo(Base):
        __tablename__ = "todo"
    
        id = Column(Integer, primary_key=True, index=True)
        title = Column(Text)
        user_id = Column(Integer, ForeignKey("user.id"))
    
    
    engine = create_engine("sqlite:///:memory:")
    SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
    db = SessionLocal()
    Base.metadata.create_all(bind=engine)
    
    ########################## DEMO ##########################
    
    db.add(User(**{"id": 1, "username": "jefmason"}))
    db.add(Todo(**{"id": 1, "title": "Make coffee", "user_id": 1}))
    db.add(Todo(**{"id": 2, "title": "Learn SQLAlchemy", "user_id": 1}))
    db.commit()
    
    result = db.query(Todo, User).join(User).all()
    
    print(result)
    # > [(<Todo(id=1 title=Make coffee user_id=1)>, <User(id=1 username=jefmason)>), (<Todo(id=2 title=Learn SQLAlchemy user_id=1)>, <User(id=1 username=jefmason)>)]
    
    print(flatten_join(result))
    # > [{'id': 1, 'title': 'Make coffee', 'user_id': 1, 'username': 'jefmason'}, {'id': 1, 'title': 'Learn SQLAlchemy', 'user_id': 1, 'username': 'jefmason'}]
    
    0 讨论(0)
提交回复
热议问题