我已经通读了 documentation for Sequelize 并了解了如何使用定义,但我不知道如何从 Sequelize 定义中读取 type/allowNull/primaryKey/autoIncrement 属性。这是我由 sequelize-auto 生成的文件的一部分;
/* jshint indent: 2 */
module.exports = function(sequelize, DataTypes) {
return sequelize.define('account', {
id: {
type: DataTypes.BIGINT,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
parent_account_id: {
type: DataTypes.BIGINT,
allowNull: false,
references: {
model: 'account',
key: 'id'
}
},
master_account_id: {
type: DataTypes.BIGINT,
allowNull: false,
references: {
model: 'account',
key: 'id'
}
},
name: {
type: DataTypes.STRING,
allowNull: false
},
// ...
最佳答案
注意模型和模型管理器的结构:
models: { user: user },
modelManager: ModelManager { models: [ user ], sequelize: [Circular] },
我认为它没有扩展用户,因为对象必须太大,所以我尝试了:
var userDefinition = this.orm["modelManager"].getModel("user");
if (userDefinition.attributes.id.type.key == "BIGINT") ...
[]
必须在 TypeScript 中使用,因为 modelManager 未在编译时初始化。
关于javascript - 如何在 Sequelize 中读取表的列属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43056702/