Consider a classic setting of two tables - user
and api_key
, represented by SQLAlchemy objects as:
class User(Base):
__tablename__ = 'user'
user_id = Column(String)
user_name = Column(String)
vioozer_api_key = Column(String, ForeignKey("api_key.api_key"))
class ApiKey(Base):
__tablename__ = 'api_key'
api_key = Column(String(37), primary_key=True)
Other fields omitted for clarity.
Suppose that I want to get the user name and the api key for a specific user id:
user, api_key = database.db_session.query(User, ApiKey)\
.join(ApiKey, User.vioozer_api_key==ApiKey.api_key)\
.filter(User.user_id=='user_00000000000000000000000000000000').first()
I get two objects - user
and api_key
, from which I can fetch user.name
and api_key.api_key
.
I would like to wrap this call with a function, which would return a single objects whose fields would be the union of the user
fields and the api_key
fields - the same way a SQL join
returns a table with the columns of both tables being joined. Is there a wayo to automatically get a object whose fields are the union of the fields of both tables?
I can define a mapper class for each Join operation, but I wonder if the mapping could be done automatically.
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'}]
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()
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.