所以我创建了一个新的 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`)
我必须运行 sails migrate 命令吗?我知道在 Rails 中我会做类似 rake db:migrate
的事情。 generate 命令后在 sails 中的过程是什么?
最佳答案
这不是错误,它只是告诉您您没有指定默认迁移策略。
只需打开config/models.js
并取消注释 migrate 所在的行,如上图所示。
就像信息“弹出”告诉你的那样,你可以选择
- 安全
- 改变
- 放弃
Drop 将删除您所有的表并重新创建它们,这对新项目很有用,并且您希望始终播种新的虚拟数据。
Alter 将尝试保留您的数据,但如果您在模型中这样做,则会对您的表进行更改。如果 sails 不能保留数据,它将被删除。
安全,顾名思义,就是最安全的。它不会对您的 table 有任何作用。
如果您想对不同的表采取不同的操作,您可以直接在您的模型中指定相同的选项,这将仅覆盖该模型的默认设置。
假设您有一个User
模型并希望保留该数据,但希望在每次您起航
时重新创建所有其他模型,您应该添加
migrate: 'safe'
直接到模型并使用drop
作为默认策略。
我个人喜欢alter
,但这可能是自以为是。
您不需要做任何其他事情。如果有一个模型并且 migrate 设置为 drop
或 alter
,它将在运行 sails lift
时被迁移。
您可以阅读有关模型设置的更多信息 here
作为旁注,您可以通过设置来查看 sails 在升降过程中对您的 table 做了什么
log: 'verbose'
在你的 config/env/development.js 文件中:
关于javascript - 如何在 sails.js 中迁移数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32980442/