【发布时间】:2020-05-25 12:01:04
【问题描述】:
使用以下方法创建模型:
sequelize-cli model:create --name User --attributes "dispName:string,email:string,phoneNum1:string,vendorId:integer"
这导致了以下迁移:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
dispName: {
type: Sequelize.STRING
},
// plus others...
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Users');
}
};
我想把自动定义的ID改成:
cognitoId: {
allowNull: false,
primaryKey: true,
type: Sequelize.STRING(100)
}
所以:
- sequelize 能否将其识别为 ID?
- 我需要在哪里进行此更改?我只能识别迁移文件。
- 模型文件没有
cognitoId的定义(或原始自动生成的id字段):我将如何获得User实例的cognitoId的值(在查询返回的数据)? - 更改自动生成的
id字段是否会产生影响? - 字段名称
id“神奇”吗?即,主键是否必须命名id? - 有没有更好的方法来做到这一点?
- 将字段类型从
Sequelize.STRING更改为Sequelize.STRING(100)是否会产生任何问题? - 为什么 sequelize-cli 生成的模型文件没有定义
id字段?
从命令行生成模型+迁移时,我找不到任何语法来指定字段的 ID 或任何其他自定义。
使用:
[Node: 12.14.1, CLI: 5.5.1, ORM: 5.21.3]
PS:NodeJS 相对较新,而 Sequelize 则完全陌生。
【问题讨论】:
标签: node.js sequelize.js sequelize-cli