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

Con*_*ech 3 javascript node.js sails.js

所以我创建了一个新的Sails.js项目,然后运行

$ sails generate api user
Run Code Online (Sandbox Code Playgroud)

喜欢加载页面建议.但是现在当我启动服务器时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`)
Run Code Online (Sandbox Code Playgroud)

我必须运行sails migrate命令吗?我知道在rails中我会做类似的事情rake db:migrate.在generate命令之后,sails中的过程是什么?

chr*_*con 9

这不是错误,它只是告诉您没有指定默认的迁移策略.

刚打开 config/models.js

在此输入图像描述

并取消注释它所说的迁移线,如上图所示.

就像"弹出窗口"告诉您的信息一样,您可以选择

  • 安全
  • 改变
  • 下降

Drop将删除所有表并重新创建它们,这对于新项目很有用,并且您希望始终为新的虚拟数据提供种子.

如果您在模型中这样做,Alter将尝试保留您的数据,但会对您的表格进行更改.如果风帆无法保存数据,则会将其删除.

正如名字所说,安全是最安全的.它对你的桌子什么都不做.

如果要对不同的表执行不同的操作,可以直接在模型中指定相同的选项,这将仅覆盖此模型的默认设置.

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

migrate: 'safe'
Run Code Online (Sandbox Code Playgroud)

直接到模型并drop用作默认策略.

alter个人喜欢,但这可能是固执己见的.

你不需要做任何其他事情.如果有模型并且migrate设置为dropor alter,则在sails lift运行时将迁移它.

您可以在此处详细了解模型设置

作为旁注,您可以通过设置在升降过程中看到帆对您的桌子做了什么

log: 'verbose'
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

  • 通常是/用户.例如/ user/name = foo应该开箱即用. (2认同)