javascriptnode.jssails.js

How to migrate the database in sails.js?


So I created a new Sails.js project, then ran

$ sails generate api user

like the loading page suggested. But now when I fire up the server with sails lift I get an error:

sails lift               

info: Starting app...

-----------------------------------------------------------------

 Excuse my interruption, but it looks like this app
 does not have a project-wide "migrate" setting configured yet.
 (perhaps this is the first time you're lifting it with models?)

 In short, this setting controls whether/how Sails will attempt to automatically
 rebuild the tables/collections/sets/etc. in your database schema.
 You can read more about the "migrate" setting here:
 http://sailsjs.org/#/documentation/concepts/ORM/model-settings.html?q=migrate

 In a production environment (NODE_ENV==="production") Sails always uses
 migrate:"safe" to protect inadvertent deletion of your data.
 However during development, you have a few other options for convenience:

 1. safe  - never auto-migrate my database(s). I will do it myself (by hand) 
 2. alter - auto-migrate, but attempt to keep my existing data (experimental)
 3. drop  - wipe/drop ALL my data and rebuild models every time I lift Sails

What would you like Sails to do?

info: To skip this prompt in the future, set `sails.config.models.migrate`.
info: (conventionally, this is done in `config/models.js`)

Is there a sails migrate command I have to run? I know in rails I would do something like rake db:migrate. What's the procedure in sails after the generate command?


Solution

  • It's not an error, it just tells you that you did not specify a default migration strategy.

    Just open config/models.js

    enter image description here

    and uncomment the line where it says migrate like in the picture above.

    Like the information "popup" tells you, you can choose between

    Drop will delete all your tables and recreate them, which is good for a new project and you want to seed new dummy data all the time.

    Alter will try to keep your data but will make changes to your tables if you do so in your models. If sails can't keep the data, it will be deleted.

    Safe is, like the name says, the safest. It will do just nothing to your tables.

    If you want to take different action for different tables, you can specify just the same options within your model directly which will overwrite the default setting for this model only.

    So say you have a User model and want to keep that data but want to have all other models recreated everytime you sails lift, you should add

    migrate: 'safe'
    

    to the model directly and use drop as default strategy.

    I like alter personally, but that might be opinionated.

    You do not need to make anything else. If there's a model and migrate is set to drop or alter, it will be migrated when sails lift is run.

    You can read more about model settings here

    As a sidenote, you can see what sails is doing exactly to your tables during lift by setting

    log: 'verbose'
    

    in your config/env/development.js file:

    enter image description here