我正在使用Node和Express,并且正在将我的对象关系映射从Mongoose (Mongo)迁移到JugglingDB (Postgres),并且很难让JugglingDB使用我定义的简单模式。
我的模式如下:
var UserToken = schema.define('UserToken', {
token: {type: String, index: true}
}, {
tablename: 'user_token'
});
var User = schema.define('User', {
email: {type: String, required: true, index: true},
password_hash: String,
first_name: String,
last_name: String,
role: {type: String, required: true, default: 'member'},
language: {type: String, default: 'en'},
api_key: String,
active: {type: Boolean, required: true, default: true},
confirmed: Date,
created: {type: Date, default: function() { return new Date() }},
modified: Date
}, {
tablename: 'users'
});
UserToken.belongsTo(User, {as: 'user', foreignKey: 'userId'});
// Define the schema in the DB if it is not there
schema.isActual(function(err, actual) {
if (!actual) {
schema.autoupdate();
}
});尝试启动节点时出现以下错误:
{ [error: relation "UserToken" does not exist]
name: 'error',
severity: 'ERROR',
code: '42P01',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
file: 'namespace.c',
line: '407',
routine: 'RangeVarGetRelidExtended' }
{ [error: relation "User" does not exist]
name: 'error',
severity: 'ERROR',
code: '42P01',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
file: 'namespace.c',
line: '407',
routine: 'RangeVarGetRelidExtended' }你能让我知道我错过了什么吗?谢谢你的帮助!
发布于 2013-10-28 18:32:38
使用table属性指定表名,如下所示:
var Organization = schema.define('Organization', {
name: String,
slug: String,
link: String
}, {
table: 'organizations'
});发布于 2013-12-01 15:21:47
对于通过JugglingDB定义的任何PostgreSQL模式,都需要自动迁移或自动更新。automigrate函数会销毁该表(如果存在),然后重新创建它。自动更新功能可更改表格。
在您的示例中,您需要在定义模式后执行以下内容:
schema.autoupdate();//或自动转换
由于自动更新和自动更新本质上是异步的,因此在创建表之前,不应访问表。在这种情况下,您可以使用Q模块或回调机制来解决此问题。使用Q模块解决方案的代码片段如下所示:
(如果有帮助,请将其加星:) ) https://gist.github.com/woonketwong/7619585
因此,向JugglingDB提交了拉取请求。修复程序在迁移PostgreSQL模式(和设置表中的属性)中更新了其规范描述。请求已被接受并合并到位于以下位置的主存储库:
https://github.com/1602/jugglingdb/commit/5702d41e2cca2383715ad6b7263b25b7da2f181c
https://stackoverflow.com/questions/18747931
复制相似问题