简体   繁体   English

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

[英]How to migrate the database in sails.js?

So I created a new Sails.js project, then ran 所以我创建了一个新的Sails.js项目,然后运行

$ 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启动服务器时,我收到一个错误:

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? 我必须运行sails migrate命令吗? I know in rails I would do something like rake db:migrate . 我知道在rails中我会做类似rake db:migrate事情。 What's the procedure in sails after the generate command? 在generate命令之后,sails中的过程是什么?

It's not an error, it just tells you that you did not specify a default migration strategy. 这不是错误,它只是告诉您没有指定默认的迁移策略。

Just open config/models.js 只需打开config/models.js

在此输入图像描述

and uncomment the line where it says migrate like in the picture above. 并取消注释它所说的迁移线,如上图所示。

Like the information "popup" tells you, you can choose between 就像“弹出窗口”告诉您的信息一样,您可以选择

  • safe 安全
  • alter 改变
  • drop 下降

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

Alter will try to keep your data but will make changes to your tables if you do so in your models. 如果您在模型中这样做, Alter将尝试保留您的数据,但会对您的表格进行更改。 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 所以说你有一个User模型并希望保留这些数据,但是想要在每次sails lift重新创建所有其他模型,你应该添加

migrate: 'safe'

to the model directly and use drop as default strategy. 直接使用drop作为默认策略。

I like alter personally, but that might be opinionated. 我个人喜欢alter ,但这可能是固执己见的。

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. 如果有模型并且迁移设置为dropalter ,则在运行sails lift时将迁移它。

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: 在config / env / development.js文件中:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM