You can iterate over rawAttributes
of the Model class
for( let key in Model.rawAttributes ){
console.log('Field Name: ', key); // this is name of the field
console.log('Field Type: ', Model.rawAttributes[key].type.key); // Sequelize type of field
}
The keys of rawAttributes
are the field names aka column names; the values of rawAttributes
are the Sequelize definitions of those columns, including properties explained in the init/define
methods., such as type
, allowNull
, defaultValue
, unique
, etc...
Remember to check rawAttributes
after you create any associations and any other setup on your Model
(Model.hasOne()
or Model.belongsTo()
, anything in your Model.associate()
function), otherwise the attributes object won't include foreign key columns.
Side-note, in Sequelize v4, Model.attributes===Model.rawAttributes
, so they are aliases for the same thing.
In Sequelize v6+, you can use Model.getAttributes()
, which is documented