【问题标题】:How can I change the type of a property in Geddy如何更改 Geddy 中的属性类型
【发布时间】:2014-01-21 19:14:13
【问题描述】:

如何更改 Geddy 中现有属性的类型?

我认为我需要在模型文件中定义属性时更改设置的类型:

this.defineProperties({
  title: {type: 'string', required: true},
  description: {type: 'text'},
  status: {type: 'boolean'}
});

我还认为我需要在迁移中更改表。我正在使用此处记录的“changeColumn”函数http://geddyjs.org/guide#models

var StatusToBoolean = function () {
  this.up = function (next) {
    this.changeColumn("step", 'status', 'boolean', function (err, data) {
      if (err) { throw err; }
      next();
    });
  };

  this.down = function (next) {
    this.changeColumn('step', 'status', 'string', function (err, data) {
      if (err) { throw err; }
      next();
    });
  };
};

exports.StatusToBoolean = StatusToBoolean;

但是,当我运行此迁移时,我收到“SQLITE_ERROR: near "ALTER"' 错误:

Hindenburg:to_do Tom$ geddy jake db:migrate  --trace
Running migrations for development environment...
Running status_to_boolean (up)
jake aborted.
Error: SQLITE_ERROR: near "ALTER": syntax error
Hindenburg:to_do Tom$ 

这让我觉得我做错了什么。我尝试了“--trace”选项(如您所见),但没有提供任何有用的信息。

我还怀疑我需要实际更改表中的一些数据(以便它可以映射到新的数据类型),但文档并不清楚如何做到这一点。

感谢任何帮助。谢谢。

【问题讨论】:

    标签: node.js geddy


    【解决方案1】:

    很遗憾,SQLite 不支持 ALTER COLUMN (http://www.sqlite.org/lang_altertable.html)。这只是 SQLite 适配器中的一个问题,它不会影响 Postgres 或 MySQL。还有一些变通方法,如下所述:How do I rename a column in a SQLite database table? 您可以编写一个执行这些步骤的迁移。当您尝试使用 SQLite 适配器执行此操作时,Geddy 的 ORM 版本 0.4.16(现在在 NPM 上)现在包含一条友好的错误消息。

    【讨论】:

    • 确实,这似乎是问题所在。看起来我应该能够通过以某种方式将该列中的所有数据从当前值修改为“真”或“假”或 Geddy 粘贴到它创建为“布尔值”的列中的任何内容来解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 2017-05-02
    • 2013-05-17
    • 2019-10-25
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    相关资源
    最近更新 更多