sql 炼金术子查询加入

sql alchemy subquery join

大家好,我无法将此 psql 查询转换为 sqlalchemy 语句。我已经在 postgresql 中测试了查询,它仍然有效,但我无法将它们转换为 sqlalchemy 语法。当然,我可以使用 sqlalchemy 执行原始 sql,但我的整个项目都使用 sqlalchemy 语法,我现在不想“打破”这个。

    select
inbox_id,
inboxparts_unseen_count,
inboxparts_ignored,
inbox_last_sent_appuser_id,
message_msg,
message_created,
message_deleted

from appuser

join inboxparts
on inboxparts_appuser_id = appuser_id

join inbox
on inbox_id = inboxparts_inbox_id

left join message on
message_id = inbox_last_message_id

join profile
on profile_appuser_id = (
    select 
        inboxparts_appuser_id
    from inboxparts
    where 
        inboxparts_appuser_id != 435
    and
        inboxparts_inbox_id = inbox_id
    )

where appuser_id = 435

我已经开始使用这个 stmt:

stmt = (
            select(
                InboxParticipants.inboxparts_unseen_count,
                InboxParticipants.inboxparts_ignored,
                Inbox.inbox_id,
                Inbox.inbox_last_sent_appuser_id,
                Message.message_msg,
                Message.message_created,
                Message.message_deleted,
            )
            .select_from(AppuserModel)
            .join(
                InboxParticipants,
                InboxParticipants.inboxparts_appuser_id
                == AppuserModel.appuser_id,
            )
            .join(
                Inbox, Inbox.inbox_id == InboxParticipants.inboxparts_inbox_id
            )
            .join(
                Message,
                Message.message_id == Inbox.inbox_last_message_id,
                isouter=True,
            )
            .join(
                Profile,
                Profile.profile_appuser_id
                == (
                    select(InboxParticipants.inboxparts_appuser_id)
                    .select_from(InboxParticipants)
                    .where(
                        and_(
                            InboxParticipants.inboxparts_appuser_id
                            != appuser_id,
                            InboxParticipants.inboxparts_inbox_id
                            == Inbox.inbox_id,
                        )
                    ),
                ),
            )
            .where(
               AppuserModel.appuser_id == appuser_id
            )
        )

“棘手”的部分是 on profile_appuser_id = ... 后跟一个子查询。非常感谢推荐

我觉得你写错级别了

这个怎么样?

stmt = (
            select(
                InboxParticipants.inboxparts_unseen_count,
                InboxParticipants.inboxparts_ignored,
                Inbox.inbox_id,
                Inbox.inbox_last_sent_appuser_id,
                Message.message_msg,
                Message.message_created,
                Message.message_deleted,
            )
            .select_from(
                select(
                    AppuserModel
                )
                .join(
                    InboxParticipants,
                    InboxParticipants.inboxparts_appuser_id
                    == AppuserModel.appuser_id,
                )
                .join(
                    Inbox, Inbox.inbox_id == InboxParticipants.inboxparts_inbox_id
                )
                .join(
                    Message,
                    Message.message_id == Inbox.inbox_last_message_id,
                    isouter=True,
                )
                .join(
                    Profile,
                    Profile.profile_appuser_id
                    == (
                        select(InboxParticipants.inboxparts_appuser_id)
                        .select_from(InboxParticipants)
                        .where(
                            and_(
                                InboxParticipants.inboxparts_appuser_id
                                != appuser_id,
                                InboxParticipants.inboxparts_inbox_id
                                == Inbox.inbox_id,
                            )
                        ),
                    ),
                )
                .where(
                   AppuserModel.appuser_id == appuser_id
                )
            )
        )