Prisma preview feature "views" in schema breaks seed

I’m using the prisma preview feature views for making prisma models for querying SQL views as if they were a relation like a table created from models. However this feature may not be totally supported yet and I’m hopeful that this inquiry can inform some positive changes in my code base.

I have created a view manually in psql and I have generated an SDL and graphql service handler. The problem is when I run a prisma reset. It seems to execute this fine but then it automatically runs my seed script and this throws an error attempting to validate my schema.

yarn rw exec seed results in the following error

error: Error validating: This line is invalid. It does not start with any known Prisma schema keyword.
  -->  schema.prisma:337
   |
336 |
337 | view DailyRevenue {
338 |   id              String           @id
   |
// ... other validation error messages of fields defined in the `view` block
error: The preview feature "views" is not known. Expected one of: fullTextSearch, fullTextIndex, tracing, metrics, orderByNulls, filteredRelationCount, fieldReference, postgresqlExtensions, clientExtensions, deno, extendedWhereUnique
  -->  schema.prisma:9
   |
 8 |   binaryTargets   = "native"
 9 |   previewFeatures = ["views"]

It seems like this should work and generate a file as the console output first looks fine then delayed it spits out the

[schema.prisma]

generator client {
  provider        = "prisma-client-js"
  binaryTargets   = "native"
  previewFeatures = ["views"]
}
// ..... other schema definitions
view DailyRevenue {
  id              String           @id
  eventId         Int
  establishmentId Int
  date            DateTime
  sumAmount       Decimal          @db.Money
  itemCount       Int
}

It seems like not all of the cli tools are receiving the --preview-feature command line flag. The following commands work fine:
yarn rw prisma migrate reset
yarn rw prisma migrate dev
yarn rw g sdl DailyRevenue

1 Like

Could you confirm your version of RedwoodJS and Prisma?

And also that you have regenerated the Prism client.

Could you clarify if “seed” is giving issues or “generating sdl”?

Since seeds write data and views are readonly, then you won’t be able to insert data into view.

I quickly did a test using RW canary and Prisma 4.11 and have views querying:

  1. Added preview feature
generator client {
  provider        = "prisma-client-js"
  binaryTargets   = "native"
  previewFeatures = ["views"]
}
  1. Added view to schema
view PostView {
  id    Int    @id
  title String
}
  1. Added migration via migrate dev --create-only and:
CREATE VIEW "PostView" AS
SELECT P.id, p.title
  FROM "POST" P;
  1. migrated

  2. Generated sdl

export const schema = gql`
  type PostView {
    id: Int!
    title: String!
  }

  type Query {
    postViews: [PostView!]! @skipAuth
    postView(id: Int!): PostView @requireAuth
  }

  input CreatePostViewInput {
    title: String!
  }

  input UpdatePostViewInput {
    title: String
  }

  type Mutation {
    createPostView(input: CreatePostViewInput!): PostView! @requireAuth
    updatePostView(id: Int!, input: UpdatePostViewInput!): PostView!
      @requireAuth
    deletePostView(id: Int!): PostView! @requireAuth
  }
`

This should actually not include CUD mutations. This is a bug/new feature.

  1. Ran rw dev and graphiql:

And was able to get the view to return data.

Oh I think I fixed it. I found some remaining dependencies in the api package.json that I hadn’t switched from @redwoodjs 3.8.0 to 4.3.0. Once I updated those remaining dependencies it worked fine. I appreciate your quick response!

For reference what I discovered that helped diagnose this was two entries in my yarn.lock for @prisma/client, one for 4.11.0 and one for 4.7.0 and they were present from a direct dependency specified in my web package.json to "@redwoodjs/auth": "3.8.0",. I needed to update all references to redwoodjs dependencies because the major version update wasn’t matched by the ^ and this direct dependency only worked for that explicit version.

Also, further clarification. It was the yarn rw exec seed that failed. The g sdl, g types, and the prisma migrate dev worked fine.

Views were a preview feature of Prisma 4.9 so it makes sense 4.7 would present that error.

Glad you got it sorted.

Views are a great addition!