简体   繁体   中英

How can I change the type of a property in Geddy

How can I change the type of an existing property in Geddy?

I believe I need to change the type as set when defining the properties in the model file:

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

I also think I need to alter the table in a migration. I'm using the 'changeColumn' function as documented here 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;

However, when I run this migration I get a 'SQLITE_ERROR: near "ALTER"' error:

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$ 

This makes me think I'm doing something wrong. I tried the '--trace' option (as you can see), but that didn't give any helpful information.

I also suspect that I need to actually change some of the data in the table (so that it can map to the new datatype), but the documentation is unclear on how to do that.

Any help is appreciated. Thanks.

Unfortunately SQLite doesn't support ALTER COLUMN ( http://www.sqlite.org/lang_altertable.html ). This is only a problem in the SQLite adapter, and it doesn't affect Postgres or MySQL. There are also workarounds, as described here: How do I rename a column in a SQLite database table? You could write a migration that performs these steps. Version 0.4.16 of Geddy's ORM (now on NPM) now includes a friendly error message when you try to do this with the SQLite adapter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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