当前位置: 动力学知识库 > 问答 > 编程问答 >

node.js - Sequelize datatype TEXT not working with mySQL

问题描述:

I am using sequelize ORM with mySQL database.

I have a model with attribute type TEXT as :

description: {

type: Sequelize.TEXT,

unique: true

},

When I am trying to create table for the corresponding model, its giving an error message as :

Unhandled rejection SequelizeDatabaseError:

ER_BLOB_KEY_WITHOUT_LENGTH: BLOB/TEXT column 'description' used in key

specification without a key length

This worked fine when used with postgreSQL.

Possible reason for this error which i could think of can be that mySQL doesn't support TEXT datatype and therefore, i have to specify it as LONGTEXT.

If I am thinking correct or is there some other reason for the same, if someone can help.

网友答案:

You are using unique: true with data-type TEXT. You can not use it. Do you delete unique: true and try again?

网友答案:

esmrkbr is correct, mySQL does not accept UNIQUE KEY on a TEXT field, you need to use VARCHAR instead (see: make text column as unique key). That being said, depending on the Database being used, you may need to explicitly specify a size for a TEXT (or BLOB) type. The documentation (http://docs.sequelizejs.com/en/latest/api/datatypes/) is pretty terse on this point and other than a link to the code, currently only has the following information:

An (un)limited length text column. Available lengths: tiny, medium, long

You can pass the size as a string argument to the type. For example, to have it defined as LONGTEXT you will need:

description: {
            type: Sequelize.TEXT('long')
        },

The code in lib/data-types.js has the following mapping to SQL for TEXT (there is a similar one for BLOB also):

TEXT.prototype.toSql = function() {
  switch (this._length.toLowerCase()) {
  case 'tiny':
    return 'TINYTEXT';
  case 'medium':
    return 'MEDIUMTEXT';
  case 'long':
    return 'LONGTEXT';
  default:
    return this.key;
  }
};
网友答案:

Define as string like this:

var filters = sequelize.define('filters', {
 description: { 
  type: DataTypes.STRING, 
  validate: { notEmpty: true }
 }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: