找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 85|回复: 1

25 回:建好所有的模型和表

[复制链接]

24

主题

22

回帖

166

积分

管理员

积分
166
发表于 2026-10-19 11:55:00 | 显示全部楼层 |阅读模式
第 25 回:建好所有的模型和表,在这节课里,我们将探讨:
  • 如何回滚迁移
  • 建好所有的模型和表

1. 回滚迁移之前的课,我们通过迁移文件,建好了Articles表。但是仔细检查下,发现id字段,忘记增加无符号了。
这种情况在开发中很常见,有两种方法解决:
  • 如果表中没啥数据,可以直接回滚迁移,也就是删除当前的文章表。接着修改迁移文件后,再次运行迁移命令,生成新的表。
  • 但有的表中,可能已经存了很多重要的数据了,就不适合整张表都删掉了重建了。这时候就适合增加另一个迁移文件,专门用来调整表的各个字段。
我们这里的文章表,都是写测试数据,一点也不重要。所以直接回滚迁移最简单。
   
sequelize db:migrate:undo运行命令后,会回滚上一次运行的迁移,也就是删掉Articles表,然后我们给迁移文件里,增加UNSIGNED
   
id: {  //...   type: Sequelize.INTEGER.UNSIGNED},再次运行迁移和种子命令
   
sequelize db:migratesequelize db:seed --seed xxx-article检查数据库中,发现无符号,已经被勾选上了。

2. 建立其他模型和表接着新建其他的模型和表了,先来建分类相关的,分类表里有名字和排序字段。
   
sequelize model:generate --name Category --attributes name:string,rank:integer打开分类表的迁移后,修改一下
  • id,增加无符号
  • name,增加不为空
  • rank,增加不为空,默认值为 1,无符号
   
async up(queryInterface, Sequelize) {  await queryInterface.createTable('Categories', {    id: {      allowNull: false,      autoIncrement: true,      primaryKey: true,      type: Sequelize.INTEGER.UNSIGNED    },    name: {      allowNull: false,      type: Sequelize.STRING    },    rank: {      allowNull: false,      defaultValue: 1,      type: Sequelize.INTEGER.UNSIGNED    },    createdAt: {      allowNull: false,      type: Sequelize.DATE    },    updatedAt: {      allowNull: false,      type: Sequelize.DATE    }  });},
2.1. 用户表继续增加用户表,运行命令
   
sequelize model:generate --name User --attributes email:string,username:string,password:string,nickname:string,sex:tinyint,company:string,introduce:text,role:tinyint要修改的迁移,我已经放在讲义里了,大家可以直接复制过去。也是增加了一些无符号,不为空、还有默认值之类的东西。
   
async up(queryInterface, Sequelize) {  await queryInterface.createTable('Users', {    id: {      allowNull: false,      autoIncrement: true,      primaryKey: true,      type: Sequelize.INTEGER.UNSIGNED    },    email: {      allowNull: false,      type: Sequelize.STRING    },    username: {      allowNull: false,      type: Sequelize.STRING    },    password: {      allowNull: false,      type: Sequelize.STRING    },    nickname: {      allowNull: false,      type: Sequelize.STRING    },    sex: {      allowNull: false,      defaultValue: 2,      type: Sequelize.TINYINT.UNSIGNED    },    company: {      type: Sequelize.STRING    },    introduce: {      type: Sequelize.TEXT    },    role: {      allowNull: false,      defaultValue: 0,      type: Sequelize.TINYINT.UNSIGNED    },    createdAt: {      allowNull: false,      type: Sequelize.DATE    },    updatedAt: {      allowNull: false,      type: Sequelize.DATE    }  });  await queryInterface.addIndex(    'Users', {      fields: ['email'],  // 要索引的字段      unique: true        // 唯一索引    });  await queryInterface.addIndex(    'Users', {      fields: ['username'],      unique: true    });  await queryInterface.addIndex(    'Users', {      fields: ['role']    });},需要重点注意的事,这里索引的写法,这就是一个固定格式。
  • Users表明要增加索引的表
  • fields里,是要索引的字段
  • unique,表示是唯一索引。如果不写unique,那就是普通索引了。

2.2. 课程表继续添加课程表,命令也给大家了,直接复制
   
sequelize model:generate --name Course --attributes categoryId:integer,userId:integer,name:string,image:string,recommended:boolean,introductory:boolean,content:text,likesCount:integer,chaptersCount:integer然后修改迁移文件
   
async up(queryInterface, Sequelize) {  await queryInterface.createTable('Courses', {    id: {      allowNull: false,      autoIncrement: true,      primaryKey: true,      type: Sequelize.INTEGER.UNSIGNED    },    categoryId: {      allowNull: false,      type: Sequelize.INTEGER.UNSIGNED    },    userId: {      allowNull: false,      type: Sequelize.INTEGER.UNSIGNED    },    name: {      allowNull: false,      type: Sequelize.STRING    },    image: {      type: Sequelize.STRING    },    recommended: {      allowNull: false,      defaultValue: false,      type: Sequelize.BOOLEAN    },    introductory: {      allowNull: false,      defaultValue: false,      type: Sequelize.BOOLEAN    },    content: {      type: Sequelize.TEXT    },    likesCount: {      allowNull: false,      defaultValue: 0,      type: Sequelize.INTEGER.UNSIGNED    },    chaptersCount: {      allowNull: false,      defaultValue: 0,      type: Sequelize.INTEGER.UNSIGNED    },    createdAt: {      allowNull: false,      type: Sequelize.DATE    },    updatedAt: {      allowNull: false,      type: Sequelize.DATE    }  });  await queryInterface.addIndex(    'Courses', {      fields: ['categoryId']    });  await queryInterface.addIndex(    'Courses', {      fields: ['userId']    });},
2.3. 章节表接着是章节表
   
sequelize model:generate --name Chapter --attributes courseId:integer,title:string,content:text,video:string,rank:integer修改迁移文件
   
async up(queryInterface, Sequelize) {  await queryInterface.createTable('Chapters', {    id: {      allowNull: false,      autoIncrement: true,      primaryKey: true,      type: Sequelize.INTEGER.UNSIGNED    },    courseId: {      allowNull: false,      type: Sequelize.INTEGER.UNSIGNED    },    title: {      allowNull: false,      type: Sequelize.STRING    },    content: {      type: Sequelize.TEXT    },    video: {      type: Sequelize.STRING    },    rank: {      allowNull: false,      defaultValue: 1,      type: Sequelize.INTEGER.UNSIGNED    },    createdAt: {      allowNull: false,      type: Sequelize.DATE    },    updatedAt: {      allowNull: false,      type: Sequelize.DATE    }  });  await queryInterface.addIndex(    'Chapters', {      fields: ['courseId']    });},
2.4. 点赞表接着建点赞表,点赞表里就只有课程 id 和用户 id 两个字段。
   
sequelize model:generate --name Like --attributes courseId:integer,userId:integer修改迁移文件
   
async up(queryInterface, Sequelize) {  await queryInterface.createTable('Likes', {    id: {      allowNull: false,      autoIncrement: true,      primaryKey: true,      type: Sequelize.INTEGER.UNSIGNED    },    courseId: {      allowNull: false,      type: Sequelize.INTEGER.UNSIGNED    },    userId: {      allowNull: false,      type: Sequelize.INTEGER.UNSIGNED    },    createdAt: {      allowNull: false,      type: Sequelize.DATE    },    updatedAt: {      allowNull: false,      type: Sequelize.DATE    }  });  await queryInterface.addIndex(    'Likes', {      fields: ['courseId']    });  await queryInterface.addIndex(    'Likes', {      fields: ['userId']    });},
2.5. 设置表最后是系统设置表
   
sequelize model:generate --name Setting --attributes name:string,icp:string,copyright:string   
async up(queryInterface, Sequelize) {  await queryInterface.createTable('Settings', {    id: {      allowNull: false,      autoIncrement: true,      primaryKey: true,      type: Sequelize.INTEGER.UNSIGNED    },    name: {      type: Sequelize.STRING    },    icp: {      type: Sequelize.STRING    },    copyright: {      type: Sequelize.STRING    },    createdAt: {      allowNull: false,      type: Sequelize.DATE    },    updatedAt: {      allowNull: false,      type: Sequelize.DATE    }  });},到这里,所有的模型和迁移文件就都搞定了。运行一下命令,生成所有的表
   
sequelize db:migrate
3. 总结一下这节课没有什么难点,大家需要掌握好如何回滚迁移,如何在迁移文件里增加不为空、无符号、增加默认值,还有如何增加索引就好了。
      
   
  

                                    

24

主题

22

回帖

166

积分

管理员

积分
166
 楼主| 发表于 2026-10-22 16:00:27 | 显示全部楼层
又重新全局安装了
npm install -g sequelize-cli
回退取消了Articles文章表的创建
npx sequelize-cli db:migrate:undo
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|知行合一

GMT+8, 2026-11-24 11:22 , Processed in 0.097618 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表