我正在使用Node with Express,并且正在将我的ORM从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' }
你可以告诉我我错过了什么吗?谢谢你的帮助!
答案 0 :(得分:2)
使用table属性指定表名,如下所示:
var Organization = schema.define('Organization', {
name: String,
slug: String,
link: String
}, {
table: 'organizations'
});
答案 1 :(得分:2)
对于通过JugglingDB定义的任何PostgreSQL模式,它需要自动迁移或自动更新。如果表存在,automigrate函数会销毁该表,然后重新创建它。自动更新功能会改变表格。
在您的情况下,您需要在架构定义后执行以下命令:
schema.autoupdate(); //或自动提供
由于automigrate和autoupdate本质上是异步的,因此在创建表之前不得访问该表。在这种情况下,您可以使用q模块或回调机制来解决此问题。使用q模块解决方案的代码片段如下所示:
(如果有帮助,请把它加星:)) https://gist.github.com/woonketwong/7619585
因此,拉取请求已提交给JugglingDB。该修补程序在迁移PostgreSQL架构(以及在表中设置属性)时更新了其规范说明。该请求已被接受并合并到主回购站:
https://github.com/1602/jugglingdb/commit/5702d41e2cca2383715ad6b7263b25b7da2f181c