简体   繁体   中英

Sequelize - How to list/view all columns of existing model

I know a simple answer could be query table with all attributes returned.

However, since the models are defined in code, I want to know is it possible to get the result without querying database? Or, if query is necessary, which query is the optimised one?

Btw, I am using Sequelize V5 and Mysql 5.7 .

You can iterate over rawAtributes of Model

for( let key in Model.rawAttributes ){
    console.log('Field: ', key); // this is name of the field
    console.log('TypeField: ', Model.rawAttributes[key].type.key); // Sequelize type of field
}

adicitionaly you have a lot information in this Model.rawAttributes, check it with Object.keys(Model.rawAttributes) and after see those values

It seems each model has an attribute "rawAttributes" which include all columns name. This may not be the official way, but it can solve my problem.

You can also use this as an answer:

for( let key of Object.keys(models.Modelname.attributes))   {
      columns.push({
          name:key,
          type:models.Modelname.attributes[key].type.key
      });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM