ExpressJS - Sequelize - 无法识别的数据类型

时间:2015-10-15 02:02:49

标签: node.js express sequelize.js

我手动创建了一个sql数据库,现在我正在尝试设置我的sequelize模型以匹配我的sql表中的列,但是我遇到了一个与无法识别的字段类型相关的模型问题。我似乎无法确定错误,如果它与我的数据库中的字段的数据类型不匹配或者sequelize无法识别我的一些代码有关。

这是我的错误:

/Users/user/Desktop/Projects/node/assistant/node_modules/sequelize/lib/model.js:90
      throw new Error('Unrecognized data type for field ' + name);
            ^
Error: Unrecognized data type for field pattern
    at null.<anonymous> (/Users/user/Desktop/Projects/node/assistant/node_modules/sequelize/lib/model.js:90:13)
    at /Users/user/Desktop/Projects/node/assistant/node_modules/sequelize/node_modules/lodash/index.js:2874:23
    at /Users/user/Desktop/Projects/node/assistant/node_modules/sequelize/node_modules/lodash/index.js:3395:24
    at /Users/user/Desktop/Projects/node/assistant/node_modules/sequelize/node_modules/lodash/index.js:3073:15
    at baseForOwn (/Users/user/Desktop/Projects/node/assistant/node_modules/sequelize/node_modules/lodash/index.js:2046:14)
    at Function.mapValues (/Users/user/Desktop/Projects/node/assistant/node_modules/sequelize/node_modules/lodash/index.js:3394:9)
    at new Model (/Users/user/Desktop/Projects/node/assistant/node_modules/sequelize/lib/model.js:74:50)
    at Sequelize.define (/Users/user/Desktop/Projects/node/assistant/node_modules/sequelize/lib/sequelize.js:577:15)
    at Object.<anonymous> (/Users/user/Desktop/Projects/node/assistant/app/models/imagesModel.js:9:24)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/Users/user/Desktop/Projects/node/assistant/app/routes.js:4:14)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)

这是我的模特:

var Sequelize      = require('sequelize');
var sequelize = new Sequelize('db', 'admin', 'pwd', {
    host: 'database-host',
    port: 3306,
    dialect: 'mysql'
});


var Images = sequelize.define('images', {
    pattern: {
        type: sequelize.STRING,
        field: 'pattern'
    },
    color: {
        type: sequelize.STRING,
        field: 'color'
    },
    imageUrl: {
        type: sequelize.STRING,
        field: 'imageUrl'
    },
    imageSource: {
        type: sequelize.STRING,
        field: 'imageSource'
    },
    description_id: {
        type: sequelize.INTEGER,
        field: 'description_id'
    }
});

module.exports = Images;

2 个答案:

答案 0 :(得分:6)

我认为你的代码应该是:

var Sequelize      = require('sequelize');
var sequelize = new Sequelize('db', 'admin', 'pwd', {
    host: 'database-host',
    port: 3306,
    dialect: 'mysql'
});


var Images = sequelize.define('images', {
    pattern: {
        type: Sequelize.STRING,
        field: 'pattern'
    },
    color: {
        type: Sequelize.STRING,
        field: 'color'
    },
    imageUrl: {
        type: Sequelize.STRING,
        field: 'imageUrl'
    },
    imageSource: {
        type: Sequelize.STRING,
        field: 'imageSource'
    },
    description_id: {
        type: Sequelize.INTEGER,
        field: 'description_id'
    }
});

module.exports = Images;

您可以参考http://docs.sequelizejs.com/en/latest/api/datatypes/http://docs.sequelizejs.com/en/latest/docs/models-definition/或此处的示例:https://github.com/thanhson1085/angular-admin-seed/blob/master/server/models/user.js

答案 1 :(得分:0)

您使用sequelize构造函数通过sequelize.define()方法定义表,同时使用分配了sequelize包的对象Sequelize来对其中的每个值执行函数您的嵌套对象。 例如,

color: {
    type: Sequelize.STRING,
    field: 'color'
}

而不是

color: {
  type: sequelize.STRING,
  field: 'color'
}