【问题标题】:Way to stop column/table change on sails lift在帆升降机上停止柱/表变化的方法
【发布时间】:2016-02-19 08:41:18
【问题描述】:

我正在使用sails 0.11.2 版。以下是

的代码

config/models.js

在我的风帆应用中

/**
 * Default model configuration
 * (sails.config.models)
 *
 * Unless you override them, the following properties will be included
 * in each of your models.
 *
 * For more info on Sails models, see:
 * http://sailsjs.org/#!/documentation/concepts/ORM
 */

module.exports.models = {
  connection: 'mysqldb',
  migrate: 'safe',
  schema : true
};

按照 documentation 的说法, migrate: 'safe' 应该停止自动迁移,并且不会更改数据库中的任何列或表。

但每次评论任何模型的属性并尝试提升sails应用程序时,与该属性关联的列都会从mysql数据库中删除。

注意:如果您需要任何其他信息或示例代码,请发表评论,以防查询。

【问题讨论】:

  • 你能给我们看一下config/models.js的所有代码吗?你用的是哪个版本的风帆?
  • @piscator 我已经用 config/models.js 的完整文件和sails 版本更新了这个问题。
  • 尝试将架构选项移动到您使用的每个模型中,架构:true,连接:'mysqldb',表名:'yourTable',属性:{}

标签: mysql node.js sails.js


【解决方案1】:

在每个模型中单独添加代码migrate: 'safe' 已解决此问题。现在 consumer.js 文件中的 Consumer 模型如下所示:

/**
* Consumer.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs        :: http://sailsjs.org/#!documentation/models
*/

module.exports = {
  migrate      : 'safe',
  autoPK       : true,
  autoCreatedAt: true,
  autoUpdatedAt: true,
  attributes   : {
    name: {
      type     : 'string',
      required : true,
      minLength: 3
    },

    email: {
      type    : 'string',
      required: true,
      unique  : true
    },
  tableName    : 'consumers'
};

虽然解决方案有效,但我相信在所有模型中不涉及此代码声誉的更好解决方案仍然存在。所以不要用这个答案将此问题标记为已解决。

【讨论】:

    猜你喜欢
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    相关资源
    最近更新 更多