我刚刚将 nodejs 包 db-migrate 安装到现有的 nodejs 项目中。关于与数据库的连接,一切似乎都已正确配置。
数据库.json:
{
"development": "postgres://blabla",
"production": "postgres://blabla"
}
迁移:
var dbm = global.dbm || require('db-migrate');
var type = dbm.dataType;
exports.up = function(db, callback) {
db.createTable('users', {
id: { type: 'int', primaryKey: true, autoIncrement: true },
username: { type: 'string', unique: true }
}, callback);
};
exports.down = function(db, callback) {
db.dropTable('users', callback);
};
每当我尝试运行 db-migrate up
(使用任何参数变体,如指定数据库文件、迁移、迁移次数等),该命令每次都会引发错误:
[ERROR] TypeError: Cannot read property '1' of null
at Class.extend.parseName (C:\Users\test\Projects\nodejs\cirio-crm\node_modules\db-migrate\lib\skeleton.js:162:17)
at Class.Skeleton.extend.init (C:\Users\test\Projects\nodejs\cirio-crm\node_modules\db-migrate\lib\migration.js:35:24)
at Class.prototype.(anonymous function) [as init] (C:\Users\test\Projects\nodejs\cirio-crm\node_modules\db-migrate\lib\class.js:36:24)
at new Class (C:\Users\test\Projects\nodejs\cirio-crm\node_modules\db-migrate\lib\class.js:49:17)
at C:\Users\test\Projects\nodejs\cirio-crm\node_modules\db-migrate\lib\migration.js:312:14
at Array.map (native)
at C:\Users\test\Projects\nodejs\cirio-crm\node_modules\db-migrate\lib\migration.js:311:35
at FSReqWrap.oncomplete (fs.js:95:15)
我试过重命名表、更改字段、弄乱 CRLF 行结尾、安装不同版本的 nodejs 和 db-migrate 等。
有什么想法吗?
最佳答案
在兜圈子大约一个小时后,我意识到迁移的命名不正确。它的第一部分只指定了日期而不是时间。要使用的正确文件名格式为 yyyyMMddhhmmss-<some text>
.
之前(坏):20160101-testmigration
之后(好):20160101000000-testmigration
错误本来可以更明确...
编辑:
正如@2Toad 所指出的,必须小心存储在应该找到迁移的文件夹中的内容。如果你有任何*.js
不遵循命名格式的文件,如helper JS文件,也会出现同样的错误。
关于node.js - NodeJS 数据库迁移类型错误 : Cannot read property '1' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35160096/