Prisma client breaks when I add a field to a model

I’m trying to add a new field to an existing model, and I keep hitting a mysterious Prisma error.

Here’s what I did:

  1. Added a single line of code to an existing model in my schema.prisma:
model Profile {
  id         Int      @id @default(autoincrement())
  // ommitted: several existing fields
  avatarCode String?  @db.VarChar() // only change
}
  1. Ran rw prisma migrate dev to generate a migration file:
-- AlterTable
ALTER TABLE "Profile" ADD COLUMN     "avatarCode" VARCHAR;
  1. Ran rw c to open the Redwood shell.
  2. Tried to query the profiles table:
> db.profile.findMany()
Uncaught PrismaClientKnownRequestError: 
Invalid `prisma.profile.findMany()` invocation:


  Failed to validate the query: `Field does not exist on enclosing type.` at `Query.findManyProfile.Profile.avatarCode`
    at PrismaClient._request (app-directory/node_modules/@prisma/client/runtime/index.js:40646:18)
    at Object.request (app-directory/node_modules/@prisma/client/runtime/index.js:39818:15) {
  code: 'P2009',
  clientVersion: '3.11.0',
  meta: {
    query_validation_error: 'Field does not exist on enclosing type.',
    query_position: 'Query.findManyProfile.Profile.avatarCode'
  }
}

When I run rw dev and navigate to a page that uses the profiles query, I see that the query fails here too:

api | 14:28:45 🌲 incoming request POST xxx /graphql
api | 14:28:45 🐛 Checking if GraphiQL Request
api | 14:28:45 🐛 Extracting GraphQL Parameters
api | 14:28:45 🐛 Processing Request
api | 14:28:46 🐛 execute-start
api | 14:28:46 🐛 query ProfilesQuery {
api |   profiles {
api |     id
api |     __typename
api |   }
api | }
api | 14:28:46 🐛 ProfilesQuery
api | 14:28:46 🐛 variables
api | 14:28:46 🐛 graphql-server GraphQL execution started: ProfilesQuery
api | 14:28:46 🐛 execute-end
api | 14:28:46 🐛 -->
api | 14:28:46 🚨 graphql-server Something went wrong.
api | 14:28:46 🌲 request completed 474ms

I assume the Prisma error message (“Field does not exist on enclosing type”) means that Prisma thinks there isn’t an avatarCode field on the Profile type. However, since I can see the avatarCode field on the Profile model when I run rw prisma studio, I’m not sure how this can be.

After searching the forum for posts about this error message, I’ve tried several fixes:

  1. Running rw g types as described here
  2. Running rw prisma generate as described here
  3. Switching from Node 16 to Node 14 as described here
  4. Removing the Prisma client as described here
  5. Running rw build as described here
  6. Upgrading my application from Redwood 0.48.0 to Redwood 0.50.0

However, none seem to have any effect. Any suggestions for how I can resolve this error?

After more reading and experimenting, I found a hacky solution: cloning the project into a new directory and using that copy. More specifically, I followed these steps:

  1. Use git clone to create a new copy of my project
  2. cd into the new directory and checkout the branch with the data model updates
  3. Run nvm use to use the Node version specified in my .nvmrc (14)
  4. Run yarn inside the new directory to install dependencies
  5. Copy the .env file from the old directory to the new directory
  6. Run rw dev (thereby regenerating the Prisma client) and then stop it
    • I imagine running rw prisma generate would also have worked.
  7. Run rw c
  8. Run db.profile.findMany() inside the Redwood shell
    • This time, it works!

Based on this experience, I’m guessing that in the old directory there was something wrong with the unique Prisma Client generated inside node_modules/.prisma/client (as described here). However, I’m still not sure what went wrong or how to fix it. I tried taking the following steps inside the old directory, but no luck:

  1. Run rm -rf node_modules to delete the entire node_modules directory, including node_modules/.prisma/client
  2. Run yarn to re-install dependencies
  3. Run rw dev and then stop it
    • When I run rw dev, the app seems to run fine in the browser, but a “Redwood Language Server” output tab pops up in my VSCode window with another mysterious error. (See addendum below.)
  4. Run rw c
  5. Run db.profile.findMany() inside the Redwood shell
    • I still see the “invalid invocation” error.

At this point, I’m not sure what’s different between the old and new directories. (Perhaps there’s a difference in some other directory ignored by git, like .redwood?) For whatever reason, though, the Prisma client works only in the new directory.

TL;DR

Fortunately, I’m able to move forward using this hacky fix, but if anyone has any insights into what the root cause might be, I’d love to hear them! It would be nice not to have to clone my project again every time I see this error. :sweat_smile:

Addendum: mysterious error in VSCode

[Info  - 8:14:54 PM] Connection to server got closed. Server will restart.
internal/modules/cjs/loader.js:937
  throw err;
  ^

Error: Cannot find module 'vscode-languageserver'
Require stack:
- app-directory/node_modules/@redwoodjs/structure/dist/language_server/RWLanguageServer.js
- app-directory/node_modules/@redwoodjs/structure/dist/language_server/start.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:934:15)
    at Module._load (internal/modules/cjs/loader.js:779:27)
    at Function.f._load (electron/js2c/asar_bundle.js:5:12913)
    at Module.require (internal/modules/cjs/loader.js:1006:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (app-directory/node_modules/@redwoodjs/structure/dist/language_server/RWLanguageServer.js:21:29)
    at Module._compile (internal/modules/cjs/loader.js:1125:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1155:10)
    at Module.load (internal/modules/cjs/loader.js:982:32)
    at Module._load (internal/modules/cjs/loader.js:823:14) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'app-directory/node_modules/@redwoodjs/structure/dist/language_server/RWLanguageServer.js',
    'app-directory/node_modules/@redwoodjs/structure/dist/language_server/start.js'
  ]
}
[Error - 8:14:54 PM] Connection to server got closed. Server will not be restarted.
```

Hey @Isaac,

I’m just taking a stab in the dark, honestly, as I came across the same error, and it caused me to scratch my head.

What I think is taking place is that the generated code in the node_modules/ at the root of your application is becoming out of sync with the one in api/node_modules/. iirc, I was able to get around this error and continue chugging along by:

  • Deleting both my-app/node_modules and my-app/api/node_modules
  • Re-generating your Prisma client yarn rw prisma generate
  • Attempt the rw c … steps as described in your OP.

Again - I could be wrong, hopefully my memory is serving correctly.

And, fwiw, deleting node_modules, api/node_modules, web/node_modules, yarn.lock and then running yarn install have been my top go-to “shit, X is broken” fix.

Edit: No idea about the addendum error, though - peculiar.

Hey, thanks for the great ideas, @realStandal! (I keep forgetting that there are three separate node_modules directories are in the project to delete. :sweat_smile:)

I tried both options:

  1. Just deleting node_modules and api/node_modules before running yarn and rw prisma generate
  2. The “shit, X is broken” option

Unfortunately, neither worked for me, which definitely surprised me.

For now, I’ll use the new directory, but I’ll keep the old directory around so that I can try out any other fixes that come to mind. And I’ll definitely add the “shit, X is broken” option to my toolkit for future issues!

1 Like

What version of Redwood are you on and are you using yarn 1 or yarn 3? There was an issue where prisma would hoist the .prisma file which would lead to a similar issue to this. Hence why deleting node_modules and api/node_modules normally would clear it up.

He was on .48 and have now updated to .50. See the last point here

1 Like

It’s really weird that rm -fr node_modules api/node_modules web/node_modules yarn.lock && yarn install doesn’t work. It’s what I do when I get weird errors like this and none of the simpler fixes works.

If you run a diff of node_modules/.prisma/client/index.d.ts in your two projects (the one that works and the one that doesn’t), do you see any relevant diff?

About the Language Server error. Do you have the RW VSCode extension installed? If so, I’d suggest to get rid of it. It’s unfortunately not working very well right now.

Also, just for completeness, could you please share the output of yarn rw info? Thanks :slight_smile:

Thanks for taking a look! To respond to your questions:

If you run a diff of node_modules/.prisma/client/index.d.ts in your two projects (the one that works and the one that doesn’t), do you see any relevant diff?

I don’t see any differences when I run this command:

git diff --no-index working-app/node_modules/.prisma/client/index.d.ts broken-app/node_modules/.prisma/client/index.d.ts

Do you have the RW VSCode extension installed? If so, I’d suggest to get rid of it.

Done!

Also, just for completeness, could you please share the output of yarn rw info ?

  System:
    OS: macOS 10.15.7
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 14.19.0 - /private/var/folders/tp/lpc352xj6x38csdqgnt_gr4c0000gn/T/xfs-f9f0b565/node
    Yarn: 3.2.0 - /private/var/folders/tp/lpc352xj6x38csdqgnt_gr4c0000gn/T/xfs-f9f0b565/yarn
  Databases:
    SQLite: 3.28.0 - /usr/bin/sqlite3
  Browsers:
    Chrome: 99.0.4844.83
    Firefox: 89.0
    Safari: 15.3
  npmPackages:
    @redwoodjs/core: ^0.50.0 => 0.50.0

And did it solve the Language Server error?

Are you running yarn3 in both projects? Have you also tried yarn1? (Please don’t ask how to switch, because I don’t know :see_no_evil:)

Yes - thanks for checking!

Yes, I’m using yarn3 in both projects. I tried yarn1 with the app on Redwood 0.48, but not with Redwood 0.50. Is Redwood 0.50 compatible with yarn1?

It is compatible with yarn 1 (I use it for my projects), but officially we recommend doing an upgrade.

Unofficially, everything still works fine :wink:

Gotcha - thanks for the heads up, @danny!

@Tobbe, I just tried yarn 1, and I’m seeing the same issue. For reference, I ran these commands:

yarn set version 1.22.17
rm -rf node_modules api/node_modules web/node_modules .yarn/install-state.gz .yarn/cache .yarn/unplugged  yarn.lock && yarn install
rw prisma generate
rw c

Since posting this issue, I’ve added a new model to the new project, and that worked fine. I haven’t tried updating an existing model again, though.

This issue isn’t blocking me (I’m just using the new project instead of the old one), but I wanted to share an update in case it’s helpful to others.

1 Like