On this page
数据表设计和迁移
创建数据迁移表
shell
npx sequelize migration:generate --name=group
1.执行完命令后,会在database / migrations / 目录下生成数据表迁移文件,然后定义
js
"use strict";
module.exports = {
up: async (queryInterface, Sequelize) => {
const { INTEGER, STRING, DATE, ENUM, TEXT } = Sequelize;
// 创建表
await queryInterface.createTable("group", {
id: {
type: INTEGER(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
},
name: {
type: STRING(30),
allowNull: false,
defaultValue: "",
comment: "群组名称",
},
avatar: {
type: STRING(200),
allowNull: true,
defaultValue: "",
},
user_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "群主id",
// 定义外键(重要)
references: {
model: "user", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
remark: {
type: TEXT,
allowNull: true,
defaultValue: "",
comment: "群公告",
},
invite_confirm: {
type: INTEGER(1),
allowNull: false,
defaultValue: 1,
comment: "邀请确认",
},
status: {
type: INTEGER(1),
allowNull: false,
defaultValue: 1,
comment: "状态",
},
created_at: DATE,
updated_at: DATE,
});
},
down: async (queryInterface) => {
await queryInterface.dropTable("group");
},
};
创建数据迁移表
shell
npx sequelize migration:generate --name=group_user
1.执行完命令后,会在database / migrations / 目录下生成数据表迁移文件,然后定义
js
"use strict";
module.exports = {
up: async (queryInterface, Sequelize) => {
const { INTEGER, DATE, STRING } = Sequelize;
// 创建表
await queryInterface.createTable("group_user", {
id: {
type: INTEGER(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
},
user_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "用户id",
// 定义外键(重要)
references: {
model: "user", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
group_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "群组id",
// 定义外键(重要)
references: {
model: "group", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
nickname: {
type: STRING(30),
allowNull: false,
defaultValue: "",
comment: "在群里的...",
},
created_at: DATE,
updated_at: DATE,
});
},
down: async (queryInterface) => {
await queryInterface.dropTable("group_user");
},
};
- 执行 migrate 进行数据库变更
shell
npx sequelize db:migrate
模型创建
js
// app/model/group.js
"use strict";
const crypto = require("crypto");
module.exports = (app) => {
const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;
// 配置(重要:一定要配置详细,一定要!!!)
const Group = app.model.define("group", {
id: {
type: INTEGER(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
},
name: {
type: STRING(30),
allowNull: false,
defaultValue: "",
comment: "群组名称",
},
avatar: {
type: STRING(200),
allowNull: true,
defaultValue: "",
},
user_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "群主id",
// 定义外键(重要)
references: {
model: "user", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
remark: {
type: TEXT,
allowNull: true,
defaultValue: "",
comment: "群公告",
},
invite_confirm: {
type: INTEGER(1),
allowNull: false,
defaultValue: 1,
comment: "邀请确认",
},
status: {
type: INTEGER(1),
allowNull: false,
defaultValue: 1,
comment: "状态",
},
created_at: DATE,
updated_at: DATE,
});
// 定义关联关系
Group.associate = function (model) {
// 一对多
Group.hasMany(app.model.GroupUser);
};
return Group;
};
js
// app/model/group_user.js
"use strict";
const crypto = require("crypto");
module.exports = (app) => {
const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;
// 配置(重要:一定要配置详细,一定要!!!)
const GroupUser = app.model.define("group_user", {
id: {
type: INTEGER(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
},
user_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "用户id",
// 定义外键(重要)
references: {
model: "user", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
group_id: {
type: INTEGER(20).UNSIGNED,
allowNull: false,
comment: "群组id",
// 定义外键(重要)
references: {
model: "group", // 对应表名称(数据表名称)
key: "id", // 对应表的主键
},
onUpdate: "restrict", // 更新时操作
onDelete: "cascade", // 删除时操作
},
nickname: {
type: STRING(30),
allowNull: false,
defaultValue: "",
comment: "在群里的...",
},
created_at: DATE,
updated_at: DATE,
});
return GroupUser;
};