Closed30

PrismaのMigrationについてひととおり知りたい

hajimismhajimism
hajimismhajimism

When adding a new field to a model with existing data, you can either:

  • Use the ? operator to mark the field as nullable
  • Provide a default value using the @default() attribute function

既存modelに絡む追加するときは、nullableとするか何らかデータ入れるかしないとまずいってことね

hajimismhajimism

いろいろなデフォルト値

model Post {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  title     String   @db.VarChar(255)
  content   String?
  published Boolean  @default(false)
  likes     Int      @default(0)

  authorId  Int?
  author    User?     @relation(fields: [authorId], references: [id])
}
hajimismhajimism
hajimismhajimism

prisma migrate dev--create-onlyをつける
migrationファイルを作成するだけで、DBには適用しない

hajimismhajimism
-- AlterTable
ALTER TABLE "Post" DROP COLUMN "published",
ADD COLUMN     "isPublished" BOOLEAN NOT NULL DEFAULT false;

--- Rename table
ALTER TABLE "Post"
RENAME COLUMN  "published" TO "isPublished";

に書き換える。そんでprisma migrate dev

hajimismhajimism
hajimismhajimism
-- CreateEnum
CREATE TYPE "AccessLevel" AS ENUM ('USER', 'ADMIN');

-- AlterTable
ALTER TABLE "User" DROP COLUMN "role",
ADD COLUMN     "role" "AccessLevel" NOT NULL DEFAULT 'USER';

-- DropEnum
DROP TYPE "Role";

-- RenameEnum
ALTER TYPE "Role" RENAME TO "AccessLevel";

-- AlterTable
ALTER TABLE "User"
ALTER COLUMN role TYPE "AccessLevel";

に書き換えてからmigrate実行

hajimismhajimism
hajimismhajimism
/*
  Warnings:

  - You are about to drop the `Post` table. If the table is not empty, all the data it contains will be lost.

*/
-- DropForeignKey
ALTER TABLE "Post" DROP CONSTRAINT "Post_authorId_fkey";

-- DropTable
DROP TABLE "Post";

-- CreateTable
CREATE TABLE "Article" (
    "id" SERIAL NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "title" VARCHAR(255) NOT NULL,
    "content" TEXT,
    "published" BOOLEAN NOT NULL DEFAULT false,
    "authorId" INTEGER,

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

-- AddForeignKey
ALTER TABLE "Article" ADD CONSTRAINT "Article_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

--- Rename "Post" table
ALTER TABLE "Post" RENAME TO "Article";

-- AlterTable
ALTER TABLE "Article" RENAME CONSTRAINT "Post_pkey" TO "Article_pkey";

-- RenameForeignKey
ALTER TABLE "Article" RENAME CONSTRAINT "Post_authorId_fkey" TO "Article_authorId_fkey";

に書き換えてから実行する。

primary keyとforeign keyのrenameを忘れずに

hajimismhajimism
hajimismhajimism
model TagOnPosts {
  // relation scalar field (used in the `@relation` attribute below)
  post   Post @relation(fields: [postId], references: [id])
  postId Int

  // relation scalar field (used in the `@relation` attribute above)
  tag   Tag @relation(fields: [tagId], references: [id])
  tagId Int

  @@id([postId, tagId])
}
hajimismhajimism

relation周りはschema書けるかどうかが難しいのでこっちではなくドキュメントを呼んだほうが良さそう

このスクラップは2024/01/04にクローズされました