Sequelize: How to check if field exists and get its Type.

Created on 10 Dec 2015  路  3Comments  路  Source: sequelize/sequelize

Hi,

I want to check if a field exists in a given table, and if it do, get its type.

Example:

  var foo = sequelize.define("Foo", {
    bar: {
      type: DataTypes.INTEGER,
    }
  });

  console.log(foo.getAttribute('bar'));
  /* Output:
    {
        type: DataTypes.INTEGER,
    }
  */
  console.log(foo.getAttribute('baz'));
  /* Output: undefined */

Most helpful comment

Model.rawAttributes.key

All 3 comments

Model.rawAttributes.key

It is returning me undefined.

Sequelize 3.13.0.

var Sequelize = require('sequelize');
var sequelize = new Sequelize('postgres', 'postgres', 'password', 
   {
        "dialect"     : "postgres",
        "timezone"    : "-03:00"
    });

var foo = sequelize.define("Foo", {
  bar: {
    type: Sequelize.INTEGER,
  }
});

sequelize
  .sync()
  .then(function() {
    console.log(foo.rawAttribute);
    /* Output: undefined */
  });


Sorry it's rawAttributes, typo on my part.

Was this page helpful?
0 / 5 - 0 ratings