Skip to main content

在 Typescript 项目中改进 MySQL 数据库的架构

Tag 模型添加到你的架构中

¥Add a Tag model to your schema

在本部分中,你将发展 Prisma 架构,然后使用 prisma migrate dev 生成迁移并将其应用到数据库。

¥In this section, you will evolve your Prisma schema and then generate and apply the migration to your database with prisma migrate dev.

出于本指南的目的,我们将对 Prisma 架构进行以下更改:

¥For the purpose of this guide, we'll make the following changes to the Prisma schema:

  1. 创建一个名为 Tag 的新模型,其中包含以下字段:

    ¥Create a new model called Tag with the following fields:

    • id:将作为模型主键的自动递增整数

      ¥id: an auto-incrementing integer that will be the primary key for the model

    • name:非空 String

      ¥name: a non-null String

    • posts:链接到 Post 模型的隐式多对多关系字段

      ¥posts: an implicit many-to-many relation field that links to the Post model

  2. 使用链接到 Tag 模型的隐式多对多关系字段的 tags 字段更新 Post 模型

    ¥Update the Post model with a tags field with an implicit many-to-many relation field that links to the Tag model

对架构进行更改后,你的架构应类似于以下架构:

¥Once you've made the changes to your schema, your schema should resemble the one below:

prisma/schema.prisma
model Post {
id Int @id @default(autoincrement())
title String @db.VarChar(255)
createdAt DateTime @default(now()) @db.Timestamp(6)
content String?
published Boolean @default(false)
authorId Int
user User @relation(fields: [authorId], references: [id])
tags Tag[]
}

model Profile {
id Int @id @default(autoincrement())
bio String?
userId Int @unique
user User @relation(fields: [userId], references: [id])
}

model User {
id Int @id @default(autoincrement())
name String? @db.VarChar(255)
email String @unique @db.VarChar(255)
post Post[]
profile Profile?
}

model Tag {
id Int @id @default(autoincrement())
name String
posts Post[]
}

要将 Prisma 架构更改应用到数据库,请使用 prisma migrate dev CLI 命令:

¥To apply your Prisma schema changes to your database, use the prisma migrate dev CLI command:

npx prisma migrate dev --name tags-model

该命令将:

¥This command will:

  1. 为迁移创建新的 SQL 迁移文件

    ¥Create a new SQL migration file for the migration

  2. 将生成的 SQL 迁移应用到数据库

    ¥Apply the generated SQL migration to the database

  3. 重新生成 Prisma 客户端

    ¥Regenerate Prisma Client

将生成以下迁移并保存在你的 prisma/migrations 文件夹中:

¥The following migration will be generated and saved in your prisma/migrations folder:

prisma/migrations/TIMESTAMP_tags_model.sql
-- CreateTable
CREATE TABLE Tag (
id SERIAL NOT NULL,
name VARCHAR(255) NOT NULL,

CONSTRAINT Tag_pkey PRIMARY KEY (id)
);

-- CreateTable
CREATE TABLE _PostToTag (
A INTEGER NOT NULL,
B INTEGER NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX _PostToTag_AB_unique ON _PostToTag(A, B);

-- CreateIndex
CREATE INDEX _PostToTag_B_index ON _PostToTag(B);

-- AddForeignKey
ALTER TABLE _PostToTag ADD CONSTRAINT _PostToTag_A_fkey FOREIGN KEY (A) REFERENCES Post(id) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE _PostToTag ADD CONSTRAINT _PostToTag_B_fkey FOREIGN KEY (B) REFERENCES Tag(id) ON DELETE CASCADE ON UPDATE CASCADE;

恭喜,你刚刚使用 Prisma Migrate 改进了数据库 🚀

¥Congratulations, you just evolved your database with Prisma Migrate 🚀