orchid-orm icon indicating copy to clipboard operation
orchid-orm copied to clipboard

Support views as relations with new migration generator

Open mordechaim opened this issue 10 months ago • 4 comments

With the new generator, is it possible to reconsider supporting views in the ORM?

The generator fails since it tries to create a table that already exists as a view.

Originally posted by @mordechaim in #47


Idea

Perhaps, we can add a virtual = true to the table definition, which skips that table from the generator; also it will not allow calling await db.myView.create().

mordechaim avatar Feb 18 '25 22:02 mordechaim

Currently, my view lives in a recurrent migration BTW.

mordechaim avatar Feb 18 '25 22:02 mordechaim

Until now I've queried the view through a computed field in the parent table, using raw SQL with row_to_json to get the same result as an ORM relation.

But, as my application started scaling, the query got exceptionally slow. It turns out that the view — which is very complex; 523 lines of code — is super slow when run as a subquery (recalculating for each row) but very fast as lateral join. With computed fields I cannot define a lateral join, only subqueries.

mordechaim avatar Feb 18 '25 22:02 mordechaim

I figured a workaround, via withSql I can pull the view into a CTE and then select through joining it to the main query. But I found it not to work, I'll open a separate issue for that.

#462

mordechaim avatar Feb 18 '25 23:02 mordechaim

Surely views a good feature to support, will do, can't promise on timing. Seems to be not too complex.

romeerez avatar Feb 23 '25 04:02 romeerez