SailsJS – 创buildlogging时如何指定string属性长度而不会出错?

我正在使用与MySQL配对的Sails 0.9.8,并想要做这样的事情

localhost:1337/player/view/<username of player> 

代替

 localhost:1337/player/view/<id of player> 

所以我把这样的东西放在模型中:

 'username' : { type: 'string', unique: true, minLength: 4, maxLength: 32, required: true }, 

但是,每当我运行帆升降机时,我都会遇到一个错误:

 { [Error: ER_TOO_LONG_KEY: Specified key was too long; max key length is 767 bytes] code: 'ER_TOO_LONG_KEY', index: 0 } 

所以在我运行模块之后,我发现这是因为默认情况下,Sails在数据库中给出的stringtypes属性长度为255。 给定的长度可以用“大小”来覆盖,但是在创buildlogging时会导致另一个错误。

 'username' : { type: 'string', unique: true, minLength: 4, maxLength: 32, size: 32, required: true }, 

创buildlogging时导致的错误:

 Error: Unknown rule: size at Object.match (<deleted>npm\node_modules\sails\node_modules\waterline\node_modules\anchor\lib\match.js:50:9) at Anchor.to (<deleted>\npm\node_modules\sails\node_modules\waterline\node_modules\anchor\index.js:76:45) at <deleted>\npm\node_modules\sails\node_modules\waterline\lib\waterline\core\validations.js:137:33 

问题是,如何在创buildlogging时指定string列的大小(以便我可以使用唯一键)而不会出现错误?

您可以通过定义types对象的自定义validation规则来解决这个问题。 具体来说,给定的问题可以通过定义一个总是返回true的自定义sizevalidation器来解决。

 // api/models/player.js module.exports = { types: { size: function() { return true; } }, attributes: { username: { type: 'string', unique: true, minLength: 4, maxLength: 32, size: 32, required: true } } }