加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

mysql – SailsJS – 如何在创建记录时指定字符串属性长度而

发布时间:2020-12-11 23:37:06 所属栏目:MySql教程 来源:网络整理
导读:我正在使用Sails 0.9.8与MySQL配对,并希望做这样的事情 localhost:1337/player/view/ 代替 localhost:1337/player/view/ 所以我在模型中加入了这样的东西: 'username' : { type: 'string',unique: true,minLength: 4,maxLength: 32,required: true }, 但是

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

localhost:1337/player/view/

代替

localhost:1337/player/view/

所以我在模型中加入了这样的东西:

'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在数据库中为string-type属性指定了255的长度.给定长度可以用’size’覆盖,但在创建记录时会导致另一个错误.

'username' : {
        type: 'string',size: 32,

创建记录时导致的错误:

Error: Unknown rule: size
at Object.match (

问题是,如何在创建记录时不指定错误的情况下指定字符串列的大小(以便我可以使用唯一键)? 最佳答案 您可以通过types对象定义custom validation rules来解决这个问题.具体而言,可以通过定义始终返回true的自定义大小验证器来解决给定问题.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读