4

I'm using Sails 0.9.8 paired with MySQL and wanting to do something like this

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

instead of

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

So I put something like this in the model:

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

But I've got an error whenever I run sails lift:

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

So after I run through the modules, I discovered that it was because by default Sails give string-type attribute a length of 255 in the database. The given length can be overridden with 'size', but it causes another error when creating a record.

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

The error caused when creating a record:

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

The question is, how do I specify the size of the string column (so that I can use the unique key) without getting an error when creating a record?

1
  • Im just guessing, but it looks like the problem is anchor doesn't have a rule defined. balderdashy/anchor can you submit an issue on the anchor repo? Commented Jan 26, 2014 at 17:20

2 Answers 2

8

You could work around this by defining custom validation rules via the types object. Specifically the given problem could be solved by defining a custom size validator that always returns true.

// 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
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

SOMEBODY GIVE THIS GUY A MEDAL!
2

The marked answer is quiet old. As per the latest sails version (1.0.2 as of the date of writing this answer),

I used the columnType attribute like this:

attributes: {

  longDescription: {
    type: 'string',
    columnType: 'text'
  }
}

1 Comment

Man you rules!! That worked out for me!!! stackoverflow.com/a/50428226/3089077

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.