How to union across multiple tables in SQLAlchemy?

前端 未结 2 1640
灰色年华
灰色年华 2021-01-13 04:30

I have a few different notification tables, and I would like to perform a union across all of them to show the user all of their notifications. However, the union is not wor

2条回答
  •  余生分开走
    2021-01-13 05:20

    I don't think this can work with a union, even supposing the query was generated as you expect. You're querying three different object types. When the ORM gets the rows back from the database, I don't see a way for it to map rows to the right class.

    A UNION in this case doesn't make much sense since the third column has a different meaning in all three tables.

    You should do the three queries separately, unless your three notification types inherit from a common ORM-mapped class. In that case SQLAlchemy supports querying the four types all at once, although not with a UNION.

提交回复
热议问题