Select 使用 SqlAlchemy Core 的模型中只有某些字段
Select only certain fields from the model using SqlAlchemy Core
我使用编码数据库库对数据库执行异步查询。该库基于 SQLAlchemy Core 表达式语言。提供所有字段的查询有效
@staticmethod
async def get_user_events_invite_list(pk: int):
events_invites = await database.fetch_all(query=event_invites.select().where(event_invites.c.to_user == pk).order_by(event_invites.c.created_at))
return ([dict(e.items()) for e in events_invites])
和return
[{'id': 18, 'from_user': 4, 'to_user': 1, 'created_at': datetime.datetime(2022, 1, 5, 14, 32, 20, 999835, tzinfo=datetime.timezone.utc), 'to_event': 1, 'status': <InviteStatus.CREATED: 'CREATED'>, 'type': <InviteType.BET: 'BET'>, 'is_active': True}, ...]
我正在尝试 select 仅来自 [=] 的某些字段(from_user、created_at、to_event 33=]相应地doc
events_invites = await database.fetch_all(query=event_invites.select(event_invites.c.from_user, event_invites.c.created_at, event_invites.c.to_event).where(event_invites.c.to_user == pk).order_by(event_invites.c.created_at))
但得到
TypeError: select() takes from 1 to 2 positional arguments but 4 were given
我哪里错了?
这个版本的查询有效
events_invites = await database.fetch_all(query=select([event_invites.c.from_user, event_invites.c.created_at, event_invites.c.to_event]).where(event_invites.c.to_user == pk).order_by(event_invites.c.created_at))
也许有人可以使用
我使用编码数据库库对数据库执行异步查询。该库基于 SQLAlchemy Core 表达式语言。提供所有字段的查询有效
@staticmethod
async def get_user_events_invite_list(pk: int):
events_invites = await database.fetch_all(query=event_invites.select().where(event_invites.c.to_user == pk).order_by(event_invites.c.created_at))
return ([dict(e.items()) for e in events_invites])
和return
[{'id': 18, 'from_user': 4, 'to_user': 1, 'created_at': datetime.datetime(2022, 1, 5, 14, 32, 20, 999835, tzinfo=datetime.timezone.utc), 'to_event': 1, 'status': <InviteStatus.CREATED: 'CREATED'>, 'type': <InviteType.BET: 'BET'>, 'is_active': True}, ...]
我正在尝试 select 仅来自 [=] 的某些字段(from_user、created_at、to_event 33=]相应地doc
events_invites = await database.fetch_all(query=event_invites.select(event_invites.c.from_user, event_invites.c.created_at, event_invites.c.to_event).where(event_invites.c.to_user == pk).order_by(event_invites.c.created_at))
但得到
TypeError: select() takes from 1 to 2 positional arguments but 4 were given
我哪里错了?
这个版本的查询有效
events_invites = await database.fetch_all(query=select([event_invites.c.from_user, event_invites.c.created_at, event_invites.c.to_event]).where(event_invites.c.to_user == pk).order_by(event_invites.c.created_at))
也许有人可以使用