0

I'm currently working on a completely new project with sailsJs, using waterline on a mysql adapter/connection.

For now, all I have are 2 tables/models. One is User, which seems to work as expected. Any changes I make to this model are applied to the mysql instance when I 'sails lift'.

However, the other model/table is Company, which other than first time creation of the table after I generated the model, seems to be ignored completely when I 'sails lift'.

Obviously, I can make the changes as I wish on the instance and not rely on sails/waterline, but it's driving me nuts anyway.

Here is the model being ignored:

module.exports = {
tableName: 'Companies',
attributes: {
    name: {
        type: 'string',
        required: true,
        minLength: 2,
        notNull: true,
        maxLength: 50
    },
    description: {
        type: 'string',
        minLength: 2,
        maxLength: 1024,
        notNull: true,
        defaultsTo: ''
    },
    industry: {
        type: 'string',
        required: true,
        defaultsTo: 'generic'
    },
    employeeCount: {
        type: 'integer',
        required: true,
        min: 1,
        defaultsTo: 1
    },
    createdBy: {
        type: 'integer',
        required: true
    },
    //address fields
    country: {
        type: 'string',
        required: true
    },
    city: {
        type: 'string',
        required: true
    },
    street: {
        type: 'string',
        required: true
    },
    areaCode: {
        type: 'string',
        required: true
    },
    phone: {
        type: 'string',
        required: true
    }

},
beforeCreate: function(company, cb){
    //do something
}
};

EDIT: This is only a problem after the table has been already created. When I dropped the table and restarted sails, the table was once again created with all of my changes, but after changing the model again, it does not apply them to the table. I'm guessing it's a waterline bug that still needs to be fixed, but I'm still wondering why it ignores one model but is perfectly fine with the other.

SirDemon
  • 1,758
  • 15
  • 24

2 Answers2

1

One issue is in your model's beforeCreate you have cb as an argument but it never gets called, you need cb(); somewhere in that function.

JohnGalt
  • 2,851
  • 2
  • 21
  • 29
1

It's recommended to specify the migrate settings explicitly. There are 3 levels:

safe : This won't make any change in DB and you need to make all changes manually. recommended for production. If you are doing sails lift with NODE_ENV=production, this setting will be applied no matter what your config is.

alter : This will try to reflect the changes made by you but it may sometimes lead to loss of data from the tables. Good for development but not recommended for production.

drop: This will straight away drop the table and recreate it.

Global migrate settings can be applied in config/models.js. For example,

module.exports.models = {
  "migrate": "safe"
}

You can also override the migrate settings in any model you need. For example,

module.exports = {
  tableName: 'Companies',
  migrate: 'alter'
  attributes: {

  }
}

Please let me know if it does not work

Mandeep Singh
  • 7,674
  • 19
  • 62
  • 104
  • 1
    Note that my question was asked before such an option was actually available. Second, using the alter option is both incredibly slow when lifting the app AND VERY unsafe due to what I believe is a bug somewhere. The slowness is due to sails loading up the entire db to memory on lift. – SirDemon Dec 02 '14 at 15:01
  • 1
    yea I noticed that after posting the answer – Mandeep Singh Dec 02 '14 at 17:42