Is there a way to define and use database views in phoenix?

Hi,
Is there a way to define and use database views in phoenix? If so, can you provide a reference or a way to do it?

Thanks,
Rajasekhar.

1 Like

I don’t think ecto cares if the relation is a table, a view or an materialized view. Just create it the way you want in the migration.

4 Likes

For creation you need to use execute/{1,2} with raw SQL query. As for querying, as @derek-zhou said, from the viewpoint of the Postgres SELECT queries, there is no difference between table and view, so with Ecto you can query it like any other table.

1 Like

Thanks, derek-zhou and hauleth. I get your point that its just about creating the views and queryeng will be the same whether its a table or a view. However, does migration files provide an option to create views or it has to be done independently?

as @hauleth has said, you would have to use raw sql wrapped inside execute/2:
https://hexdocs.pm/ecto_sql/Ecto.Migration.html#execute/2

1 Like

Ok, Thanks derek-zhou.