On this page
数据表设计和迁移
创建数据迁移表
shell
npx sequelize migration:generate --name=category
1.执行完命令后,会在database / migrations / 目录下生成数据表迁移文件,然后定义
js
"use strict";
module.exports = {
up: (queryInterface, Sequelize) => {
const { INTEGER, STRING, DATE, ENUM, TEXT } = Sequelize;
return queryInterface.createTable("category", {
id: {
type: INTEGER(20),
primaryKey: true,
autoIncrement: true,
},
title: {
type: STRING(100),
allowNull: false,
defaultValue: "",
comment: "分类名称",
},
cover: {
type: STRING,
allowNull: true,
defaultValue: "",
comment: "分类图标",
},
desc: {
type: TEXT,
allowNull: false,
defaultValue: "",
comment: "分类描述",
},
created_time: DATE,
updated_time: DATE,
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable("category");
},
};
- 执行 migrate 进行数据库变更
shell
npx sequelize db:migrate
模型创建
js
// app/model/category.js
module.exports = (app) => {
const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;
const Category = app.model.define("category", {
id: {
type: INTEGER(20),
primaryKey: true,
autoIncrement: true,
},
title: {
type: STRING(100),
allowNull: false,
defaultValue: "",
comment: "分类名称",
},
cover: {
type: STRING,
allowNull: true,
defaultValue: "",
comment: "分类图标",
},
desc: {
type: TEXT,
allowNull: false,
defaultValue: "",
comment: "分类描述",
},
created_time: DATE,
updated_time: DATE,
});
return Category;
};