After a successful migration where I updated my sc...
# orm-help
s
After a successful migration where I updated my schema with a new model, and a corresponding related field to an existing model, I’m getting this error:
Copy code
Invalid `prisma.book.findMany()` invocation:

{
  include: {
    moods: true,
    ~~~~~
?   pairings?: true,
?   _count?: true
  }
}


Unknown field `moods` for include statement on model Book. Available options are listed in green. Did you mean `_count`?
This doesn’t make sense to me because
npx prisma migrate dev
went fine, I’ve seeded my database using the new model name, and I’ve confirmed in postgres that the new table (
Mood
) is there. Why would I get this error with the new field name and not the old field name? Running prisma 3.11.0 & @prisma/client 3.11.0
For context, all I want to do is update the term “Pairing” to “Mood” in my app. This is proving to be very difficult. Here are the models in my schema with comments for context:
Copy code
// This model was already here, all I did was add the field "moods"
model Book {
  title          String
  gbid           String
  createdAt      DateTime
  slug           String    @unique
  authors        String[]
  categories     String[]
  publishedDate  String?
  smallThumbnail String?
  thumbnail      String?
  description    String?
  id             String    @id
  bookmarks      Int       @default(0)
  pairings       Pairing[]
  moods          Mood[]
}

// This model was already here, I left it untouched, will delete after the new model starts working
model Pairing {
  createdAt         DateTime
  playlistType      String
  playlistId        String
  playlistName      String
  likes             Int      @default(0)
  bookId            String
  id                String   @id @default(cuid())
  playlistThumbnail String?
  playlistCreators  String[]
  totalTracks       Int      @default(0)
  book              Book     @relation(fields: [bookId], references: [id])
}

// Here is the new model, same fields as the Pairing model
model Mood {
  createdAt         DateTime
  playlistType      String
  playlistId        String
  playlistName      String
  likes             Int      @default(0)
  bookId            String
  id                String   @id @default(cuid())
  playlistThumbnail String?
  playlistCreators  String[]
  totalTracks       Int      @default(0)
  book              Book     @relation(fields: [bookId], references: [id])
}
Finally, here is the migration.sql file:
Copy code
-- CreateTable
CREATE TABLE "Mood" (
    "createdAt" TIMESTAMP(3) NOT NULL,
    "playlistType" TEXT NOT NULL,
    "playlistId" TEXT NOT NULL,
    "playlistName" TEXT NOT NULL,
    "likes" INTEGER NOT NULL DEFAULT 0,
    "bookId" TEXT NOT NULL,
    "id" TEXT NOT NULL,
    "playlistThumbnail" TEXT,
    "playlistCreators" TEXT[],
    "totalTracks" INTEGER NOT NULL DEFAULT 0,

    CONSTRAINT "Mood_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "Mood" ADD CONSTRAINT "Mood_bookId_fkey" FOREIGN KEY ("bookId") REFERENCES "Book"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
n
Hey 👋 Just for clarifying, was your Prisma schema in sync with PrismaClient?
npx prisma generate
would sync your PrismaClient with schema file
s
Hi @Nurul! Throughout multiple attempts, I remember running
npx prisma generate
both before and after
npx prisma migrate dev
, still without success. I ended up just leaving my models named as they were. Does
npx prisma generate
always need to be run in conjunction with
npx prisma migrate dev
?
n
Hey 👋 If you execute
npx prisma migrate dev
it should ideally automatically run
prisma generate
command so you shouldn’t have to execute it again
s
@Nurul It turns out that this issue was also caused by what I described in this thread: https://prisma.slack.com/archives/CA491RJH0/p1648485267498929 I only just learned how to investigate if the PrismaClient is out of sync with the schema, and that is my best guess as to what happened. Even though I was getting a success message after
npx prisma migrate dev
and
npx prisma generate
, I was not aware that my project was still synced to an outdated PrismaClient.
👍 1
n
Thank you so much for describing this in detail, I’ll make sure to go through it
s
Excellent. There was some oversight on my part, but in a developer experience sense, I feel like there was some ambiguity as to what was happening, and errors that weren’t reporting where things were going wrong. To illustrate my experience more, I ended up adding Prisma to my stack based on a popular tutorial for SvelteKit. Tutorials like this show how to use PrismaClient, but only in the context of a small example where it is instantiated once. This Prisma help center article provides a great solution, but frames it specifically around use with Next.js. Lots of frameworks now offer ways to leverage Prisma via custom endpoints (where PrismaClient will be imported in many different files), and I imagine that many devs have run into the same issue I did. I would advocate for this “saving PrismaClient on a global object” approach to be made more front-and-center in the docs.
👍 1