如何在sails.js中迁移数据库?

于是我创build了一个新的Sails.js项目,然后运行

$ sails generate api user 

就像build议的加载页面一样。 但是现在,当我用sails lift服务器时,出现一个错误:

 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`) 

有一个帆船迁移命令我必须运行? 我知道在轨道上我会做类似rake db:migrate 。 生成命令后,sails中的过程是什么?

这不是一个错误,它只是告诉你,你没有指定一个默认的迁移策略。

只要打开config/models.js

在这里输入图像描述

并取消上面所说的迁移路线。

就像“popup”告诉你的信息,你可以select

  • 安全
  • 改变
  • 下降

Drop将会删除所有的表格并重新创build它们,这对于一个新的项目来说是很好的,并且你想要一直播种新的虚拟数据。

Alter将尽量保留您的数据,但如果您在模型中进行更改,则会对表格进行更改。 如果sails不能保存数据,它将被删除。

正如名字所说, 安全是最安全的。 它不会对你的桌子做任何事情。

如果您想针对不同的表格执行不同的操作,则可以直接在模型中指定相同的选项,仅覆盖此模型的默认设置。

所以说你有一个User模型,并希望保留这些数据,但希望所有其他模型重新创build每次你sails lift ,你应该添加

 migrate: 'safe' 

直接到模型并使用drop作为默认策略。

我个人喜欢alter ,但这可能是自以为是的。

你不需要做任何事情。 如果有一个模型,并且迁移被设置为dropalter ,则在运行sails lift时它将被迁移。

您可以在这里阅读更多关于模型设置

作为一个旁注,你可以看到在升降过程中通过设置什么帆正在做你的桌子

 log: 'verbose' 

在你的config / env / development.js文件中:

在这里输入图像描述