gpt4 book ai didi

node.js - 为什么 sequelize-cli 不在模型文件中包含 ID?

转载 作者:行者123 更新时间:2023-12-03 22:26:10 25 4
gpt4 key购买 nike

当我运行以下命令时:

sequelize-cli model:create --name User --attributes "dispName:string,email:string,phoneNum1:string"

我最终得到以下迁移文件:
'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
},
email: {
type: Sequelize.STRING
},
phoneNum1: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Users');
}
};

以及以下模型文件:
'use strict';
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
dispName: DataTypes.STRING,
email: DataTypes.STRING,
phoneNum1: DataTypes.STRING
}, {});
User.associate = function(models) {
// associations can be defined here
};
return User;
};

问题:
  • 为什么模型文件不包含 id 的定义?
  • 如果我更改主键字段的名称和/或定义(键将在外部生成并在保存之前在对象上设置),那么我是否必须在模型中包含 ID 字段的定义文件?为什么/为什么不?

  • 版本:
    [Node: 12.14.1, CLI: 5.5.1, ORM: 5.21.3]

    以下不回答我的问题:
  • Should Sequelize migrations update model files?
  • 最佳答案

    如果你没有在你的模型中声明 PK sequelize 假设你有 id PK。这是设计使然。是的,您可以在模型中重命名您的 PK。只是不要忘记根据数据库中的真实 PK 在模式中正确设置 PK。

    关于node.js - 为什么 sequelize-cli 不在模型文件中包含 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60152711/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com