Egg.js 技巧
# Sequelize
# sequelize-cli
需要 Migrations (opens new window) 来帮我们管理数据结构的变更(在项目的演进过程中,每一个迭代都有可能对数据库数据结构做变更,怎样跟踪每一个迭代的数据变更,并在不同的环境(开发、测试、CI)和迭代切换中,快速变更数据结构呢?) sequelize 提供了 sequelize-cli (opens new window) 工具来实现 Migrations (opens new window)
sequelize <command>
Commands:
sequelize db:migrate Run pending migrations
sequelize db:migrate:schema:timestamps:add Update migration table to have timestamps
sequelize db:migrate:status List the status of all migrations
sequelize db:migrate:undo Reverts a migration
sequelize db:migrate:undo:all Revert all migrations ran
sequelize db:seed Run specified seeder
sequelize db:seed:undo Deletes data from the database
sequelize db:seed:all Run every seeder
sequelize db:seed:undo:all Deletes data from the database
sequelize db:create Create database specified by configuration
sequelize db:drop Drop database specified by configuration
sequelize init Initializes project
sequelize init:config Initializes configuration
sequelize init:migrations Initializes migrations
sequelize init:models Initializes models
sequelize init:seeders Initializes seeders
sequelize migration:generate Generates a new migration file [aliases: migration:create]
sequelize model:generate Generates a model and its migration [aliases: model:create]
sequelize seed:generate Generates a new seed file [aliases: seed:create]
Options:
--version Show version number [boolean]
--help Show help [boolean]
Please specify a command
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 升级数据库
npx sequelize db:migrate
# 如果有问题需要回滚,可以通过 `db:migrate:undo` 回退一个变更
# npx sequelize db:migrate:undo
# 可以通过 `db:migrate:undo:all` 回退到初始状态
# npx sequelize db:migrate:undo:all
2
3
4
5
6
# 错误处理
2020-10-23 15:20:10,104 ERROR 124768 [-/127.0.0.1/-/8ms POST /api/v1/video] nodejs.SequelizeDatabaseError: Incorrect string value: '\xE5\x8F\x8D\xE5\xAF\xB9...' for column 'introduce' at row 1
_ at Query.formatError (D:\Code\Work\wxwl-resource-share\node_modules_sequelize@6.3.5@sequelize\lib\dialects\mysql\query.js:239:16)_ _ at Query.run (D:\Code\Work\wxwl-resource-share\node_modules_sequelize@6.3.5@sequelize\lib\dialects\mysql\query.js:54:18)_ _ at processTicksAndRejections (internal/process/task_queues.js:97:5)_ name: "SequelizeDatabaseError" parent: {"code":"ER_TRUNCATED_WRONG_VALUE_FOR_FIELD","errno":1366,"sqlState":"HY000","sqlMessage":"Incorrect string value: '\xE5\x8F\x8D\xE5\xAF\xB9...' for column 'introduce' at row 1","sql":"INSERT INTO
videos
(id
,category_id
,title
,introduce
,detail
,create_date
) VALUES (DEFAULT,?,?,?,?,?);","parameters":[1,"test11","tess 反对 t","231231","2020-10-23 07:20:10"]} original: {"code":"ER_TRUNCATED_WRONG_VALUE_FOR_FIELD","errno":1366,"sqlState":"HY000","sqlMessage":"Incorrect string value: '\xE5\x8F\x8D\xE5\xAF\xB9...' for column 'introduce' at row 1","sql":"INSERT INTOvideos
(id
,category_id
,title
,introduce
,detail
,create_date
) VALUES (DEFAULT,?,?,?,?,?);","parameters":[1,"test11","tess 反对 t","231231","2020-10-23 07:20:10"]} sql: "INSERT INTOvideos
(id
,category_id
,title
,introduce
,detail
,create_date
) VALUES (DEFAULT,?,?,?,?,?);" parameters: [1,"test11","tess 反对 t","231231","2020-10-23 07:20:10"] pid: 124768 hostname: DESKTOP-FUCU627