Comment on page
数据建模
该功能为Beta版本,不推荐生产环境使用!!!
你也可以使用Navicat等数据库管理工具进行表结构设计。
01功能介绍 如何用飞布设计数据库?

数据建模
1,切换到“数据建模”页签
2,选择数据源,例如
todo
数据库3,点击“
”切换到数据建模功能页

数据建模支持两种模式:普通视图和源码视图,分别适用于新手开发者和熟悉Prisma的开发者。点击右上角的图标
和
,可切换两种视图。


MongoDB数据库暂不支持模型设计。
- 1.点击数据建模右侧的“+”,双击输入表名
- 2.点击右侧面板顶部的“+”,输入字段名称和类型
- 3.点击字段行后的“?”,设置字段为数组或是否为空
- 4.点击字段行后的“@”,为字段增加描述
- 5.
- 6.点击顶部的“迁移”按钮,保存修改
有两个方式可以删除表,一是选中表后,点击顶部的“删除”按钮,二是在左侧列表右击,点击“删除”。
源码视图展示prisma schema源文件,同时支持语法提醒和高亮展示,你可以用它实现任意形式的数据建模。

源码视图
一对一(1-1)关系是指最多一个记录可以在关系的两边连接。在下面的示例中,User和Profile:
model User {
id Int @id @default(autoincrement())
profile Profile?
}
model Profile {
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id])
userId Int @unique // relation scalar field (used in the `@relation` attribute above)
}
在飞布中可以得到对应的ER图,如下:

一对一的关系图
在关系型数据库中可以定义多个字段的一对一关联:
model User {
firstName String
lastName String
profile Profile?
@@id([firstName, lastName])
}
model Profile {
id Int @id @default(autoincrement())
user User @relation(fields: [userFirstName, userLastName], references: [firstName, lastName])
userFirstName String // relation scalar field (used in the `@relation` attribute above)
userLastName String // relation scalar field (used in the `@relation` attribute above)
@@unique([userFirstName, userLastName])
}
在飞布中可以得到对应的ER图,如下:

多字段一对一的关系图
一对多(1-n)关系是指关系一侧的一个记录可以连接到另一侧的零或多个记录的关系。在以下示例中,User和Post模型之间有一个一对一的关系:
model User {
id Int @id @default(autoincrement())
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
author User @relation(fields: [authorId], references: [id])
authorId Int
}
在飞布中可以得到对应的ER图,如下:

一对多的关系图
在关系型数据库中可以定义多个字段的一对多关联:
model User {
firstName String
lastName String
post Post[]
@@id([firstName, lastName])
}
model Post {
id Int @id @default(autoincrement())
author User @relation(fields: [authorFirstName, authorLastName], references: [firstName, lastName])
authorFirstName String // relation scalar field (used in the `@relation` attribute above)
authorLastName String // relation scalar field (used in the `@relation` attribute above)
}
在飞布中可以得到对应的ER图,如下:

多字段一对多的关系图
多对多关系定义了三种模型:
- 1.两个具有多对多关系的模型,如Category和Post
- 2.一个表示关系表的模型,例如基础数据库中的 CategoriesOnPosts(有时 也称为JOIN、链接或数据透视表)
在本例中,表示关系表的模型定义了描述Post/Category关系的其他字段-谁分配了类别(assignedBy),以及何时分配了类别(assignedAt):
model Post {
id Int @id @default(autoincrement())
title String
categories CategoriesOnPosts[]
}
model Category {
id Int @id @default(autoincrement())
name String
posts CategoriesOnPosts[]
}
model CategoriesOnPosts {
post Post @relation(fields: [postId], references: [id])
postId Int // relation scalar field (used in the `@relation` attribute above)
category Category @relation(fields: [categoryId], references: [id])
categoryId Int // relation scalar field (used in the `@relation` attribute above)
assignedAt DateTime @default(now())
assignedBy String
@@id([postId, categoryId])
}
在飞布中可以得到对应的ER图,如下:

多对多的关系图