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

问题描述:

所以我创建了一个新的Sails.js项目,然后跑如何在sails.js中迁移数据库?

$ sails generate api user 

像加载页面上提示。但现在当我启动服务器与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

enter image description here

,并取消地方说,像迁移在上面的图片就行了。

像信息 “弹出” 告诉你,你可以选择

安全之间
  • 改变
  • 下降

将删除所有表并重新创建它们,这对一个新项目很有好处,并且您希望始终播种新的虚拟数据。

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

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

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

所以说你有一个User模型,并要保留数据,但想拥有所有其他型号重建每次你sails lift,你应该直接添加

migrate: 'safe' 

的模型,并使用drop为默认策略。

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

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

可以通过在你的config/ENV /开发设置

log: 'verbose' 

阅读更多关于模型设置here

一点题外话,你可以看到什么帆的升降过程中究竟做给你的表。 js文件:

enter image description here

+0

谢谢。我用'安全'去了。有一个API端点,我可以查看用户'sails生成API用户命令?试过'/ api/users'和'/ users',但得到404页面 –

+2

通常是/ user。 E.g/user/name = foo应该开箱即用。 – baao

+0

单数。得到它了。那就行了 –