Skip to content

Add support for database views #678

Closed

Description

@divyenduz

Problem

In a database, a view is the result set of a stored query on the data, which the database users can query just as they would in a persistent database collection object. Unlike ordinary base tables in a relational database, a view does not form part of the physical schema: as a result set, it is a virtual table computed or collated dynamically from data in the database when access to that view is requested. Changes applied to the data in a relevant underlying table are reflected in the data shown in subsequent invocations of the view.

Prisma does not currently support database views.

Suggested solution

No suggestion.

Alternatives

The current workaround available is to manage views manually and query views by using the raw SQL API.

Or use the documented workarounds https://www.prisma.io/docs/guides/database/advanced-database-tasks/sql-views-postgres or https://www.prisma.io/docs/guides/database/advanced-database-tasks/sql-views-mysql but make sure to read the "Limitations" section properly!

Alternatively you can treat a "fake model" to your Prisma schema that has the same structure than the view results. Note that future Introspection might drop this "model" and future Migrations might try to create a table of that name - so make sure to handle that.

Additional context

Frameworks like Ruby on Rails don't support views out of the box, but there are 3rd party libraries available (scenic for Rails).

Original issue

Prisma 2 init doesn't print MySQL views into the Prisma schema, this issue tracks support for introspection of views for being able to use it with brownfield projects.

This ties into this larger epic: #156

We also need to (possibly) add support for creating views in Prisma schema file, this issue tracks that for the time being before we have clarity on this topic and we can split it into multiple issues.

This issue also tracks views across all connectors:

  • MySQL
  • Postgres
  • SQLite

Activity

  1. added
    kind/feature
    and removed
    kind/feature
    on Oct 7, 2019
  2. removed their assignment
    on Dec 17, 2019
  3. jeffvandyke commented on Apr 2, 2020

    @jeffvandyke
  4. divyenduz commented on Apr 3, 2020

    @divyenduz
    Author
  5. changed the title [-]Support for MySQL views[/-] [+]Support for views[/+] on Apr 3, 2020
  6. changed the title [-]Support for views[/-] [+]Add support for database views[/+] on May 25, 2020
  7. felinto-dev commented on Jun 1, 2020

    @felinto-dev
  8. 121 remaining items

  9. jameskennethrobinson commented on Feb 7, 2023

    @jameskennethrobinson
  10. janpio commented on Feb 7, 2023

    @janpio
    Contributor

    Yes, you only need to call the refresh function to refresh the materialized views manually for now via raw queries. We might make that more convenient later.

  11. janpio commented on Feb 7, 2023

    @janpio
    Contributor
  12. jamiter commented on Mar 22, 2023

    @jamiter
  13. added
    status/is-preview-feature
    on Mar 27, 2023
  14. janpio commented on Mar 31, 2023

    @janpio
    Contributor

    In Prisma 4.12.0 we added support for creating files based on the introspected views with their SQL query they run, and we are looking for feedback if this works as expected. More information here: #17335 (comment)

  15. igalklebanov commented on Dec 26, 2025

    @igalklebanov
    Contributor

    Hey 👋

    Closing this since there's view support (in preview).

  16. locked and limited conversation to collaborators on Dec 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/featurestatus/is-preview-featuretopic: database-functionalitytopic: introspectiontopic: schematopic: views

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions