1

When setting up sails models to use an existing MySql database with string primary keys (PK), the PK is no longer generated automatically. Am I missing something or do I have to implement the PK generation in beforeCreate? If I were to implement the PK generation, I'm not sure how to avoid having key collisions with the existing keys from the legacy db.

Calling Cat.create().exec(console.log) throws the following error:

Invalid attributes sent to User:
• id
  • `undefined` should be a string (instead of "null", which is a object)
  • "required" validation rule failed for input: null

The model has the following code:

module.exports = {
connection: 'laraschMysql',
schema: true,
identity: 'Contest',
tableName: 'Contest',
migrate: 'safe',
autoPK: false,
autoCreatedAt: false,
autoUpdatedAt: false,
autoUpdatedAt: false,
  attributes: {
    id: {
      type: 'string',     //varchar(255) in the db                                                                       
      autoIncrement:true, // not working?
      primaryKey: true,
      required: true
    }
}

Put more generally: What are the usual problems / stumbling blocks / issues when using a legacy database with a new sails application?

Are there any good guides ( the sails documentation doesn't seem to cover the subject too thoroughly ) to setting up a legacy db with sails?

danba
  • 842
  • 11
  • 32

1 Answers1

0

Based on Sails.js documentation autoIncrement must always be type: 'integer'

autoIncrement

Sets up the attribute as an auto-increment key. When a new record is added to the model, if a value for this attribute is not specified, it will be generated by incrementing the most recent record's value by one. Note: Attributes which specify autoIncrement should always be of type: 'integer'. Also, bear in mind that the level of support varies across different datastores. For instance, MySQL will not allow more than one auto-incrementing column per table.

Bonanza
  • 1,601
  • 14
  • 23