【问题标题】:node-orm sync to Alter Tables (similar to DataMapper.auto_upgrade)node-orm 同步到 Alter Tables(类似于 DataMapper.auto_upgrade)
【发布时间】:2012-05-23 22:33:12
【问题描述】:

我正在使用 node-orm 来尝试设置我的数据库。这是模型代码。

db = orm.connect("mysql", client, (success, db) ->
  Strain = db.define("strain",
    name:
      type: "string"
      validations: [ orm.validators.unique() ]
    old_body:
      type: "string"
    body:
      type: "string"
    created_at:
      type: "date"
    update_at:
      type: "date"
  )
  Strain.sync()
) 

我将 /orm/lib/databases/mysql.js 文件更改为 console.log 返回的同步信息。

this._client.query(_query, function (err, info) {
  console.log(err);
  console.log(info);
  console.log("collection synced");
});

第一次运行 Strain.sync() 时,这是输出。

null

{ affectedRows: 0,
  insertId: 0,
  serverStatus: 2,
  warningCount: 0,
  message: '',
  setMaxListeners: [Function],
  emit: [Function],
  addListener: [Function],
  on: [Function],
  once: [Function],
  removeListener: [Function],
  removeAllListeners: [Function],
  listeners: [Function] }

collection synced

现在表格已按原样创建。当我重新启动服务器并再次运行 Strain.sync() 时,输出如下:

null

{ affectedRows: 0,
  insertId: 0,
  serverStatus: 2,
  warningCount: 1,
  message: '',
  setMaxListeners: [Function],
  emit: [Function],
  addListener: [Function],
  on: [Function],
  once: [Function],
  removeListener: [Function],
  removeAllListeners: [Function],
  listeners: [Function] }

collection synced

警告计数跳到“1”,但err为null,message为空。

我需要弄清楚如何更改模型并添加新属性,例如“deleted_at”,并在不丢失数据的情况下更新表。我知道我可以做 Strain.sync(force: true),但这会删除表然后重新创建它。我只是想更新表,类似于 DataMapper 的 auto_upgrade 函数。

有没有办法用 node-orm 或任何适用于 nodejs 的 ORM 来做到这一点?

【问题讨论】:

    标签: node.js orm coffeescript


    【解决方案1】:

    很确定当前版本的 sync() 只创建表,不更新。

    注意:sync() 目前只创建表;表修改将在即将发布的版本中添加。

    https://github.com/dresende/node-orm#creating-the-model-on-the-database

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      • 2017-03-18
      相关资源
      最近更新 更多