1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Prismaのschema.prismaにて、フィールドを更新するときはマイグレーションファイルを作成しよう

Posted at

この記事は、僕がいつもschema.prismaを更新するときにエラーを出してしまうので、忘備録としてマイグレーションの手順を残しておきます。

schema.prismaにて定義され値得るモデルに新しいフィールドを追加する場合を考えましょう。

例えば、次のようにUserモデルにnicknameフィールドを追加したいとします。

model User {
  id    String @id @default(cuid())
  email String @unique
  name  String @default("")

  nickname String? @default("") // 新しく追加したいフィールド
}

環境変数DATABASE_URLは、ローカル環境のものに設定されていることを確認します。

DATABASE_URL="postgresql://postgres:postgres@localhost:5432/postgres?connection_limit=40"

この状態でnpm run setupを実行すると、エラーが発生してしまいます。

npm run setupprisma generate && prisma migrate deploy && prisma db seedを実行しています。これは、Remix Blues Stackのプロジェクトを作成したときに自動的に用意されるスクリプトです。

Running seed command `ts-node --require tsconfig-paths/register prisma/seed.ts` ...
PrismaClientKnownRequestError: 
Invalid `prisma.user.create()` invocation in
/workspaces/remix-app/prisma/seed.ts:17:34

  14 const hashedPassword = await bcrypt.hash("racheliscool", 10);
  15 
  16 // create an admin user
→ 17 const user = await prisma.user.create(
The column `nickname` does not exist in the current database.
    at RequestHandler.handleRequestError (/workspaces/remix-app/node_modules/@prisma/client/runtime/index.js:34310:13)
    at RequestHandler.request (/workspaces/remix-app/node_modules/@prisma/client/runtime/index.js:34293:12)
    at async PrismaClient._request (/workspaces/remix-app/node_modules/@prisma/client/runtime/index.js:35273:16)
    at async seed (/workspaces/remix-app/prisma/seed.ts:17:16) {
  code: 'P2022',
  clientVersion: '4.6.1',
  meta: { column: 'nickname' }
}

An error occurred while running the seed command:
Error: Command failed with exit code 1: ts-node --require tsconfig-paths/register prisma/seed.ts

Prismaのエラーメッセージを見ると、nicknameフィールドが存在しないということがわかります。

Prismaに熟練している開発者の方なら何が問題かもうすでにお気づきかと思います。そうです。マイグレーションファイルが作成されていないのです。

最初にマイグレーションファイルを作成しましょう。

node ➜ /workspaces/remix-app (main) $ npx prisma migrate dev --name "add-nickname"
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "postgres", schema "public" at "localhost:5432"
...
The following migration(s) have been created and applied from new schema changes:

migrations/
  └─ 20230225141819_add_nickname/
    └─ migration.sql

Your database is now in sync with your schema.

✔ Generated Prisma Client (4.6.1 | library) to ./node_modules/@prisma/client in 157ms

マイグレーションファイルが作成されたことが確認できたら、npm run setupを実行します。

node ➜ /workspaces/remix-app (main) $ npm run setup

> setup
> prisma generate && prisma migrate deploy && prisma db seed

Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma

✔ Generated Prisma Client (4.6.1 | library) to ./node_modules/@prisma/client in 82ms
You can now start using Prisma Client in your code. Reference: https://pris.ly/d/client

import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()


warn Versions of prisma@4.10.1 and @prisma/client@4.6.1 don't match.
This might lead to unexpected behavior.
Please make sure they have the same version.
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "postgres", schema "public" at "localhost:5432"

7 migrations found in prisma/migrations


No pending migrations to apply.
Environment variables loaded from .env
Running seed command `ts-node --require tsconfig-paths/register prisma/seed.ts` ...
Database has been seeded. 🌱

🌱  The seed command has been executed.

これで完了です。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?