关系不存在 – JugglingDB与Postgres适配器中的错误

我正在使用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' } 

你能让我知道我失踪了吗? 谢谢你的帮助!

使用table属性指定表名,如下所示:

 var Organization = schema.define('Organization', { name: String, slug: String, link: String }, { table: 'organizations' }); 

对于通过JugglingDB定义的任何PostgreSQL模式,它需要自动化或自动化。 automigrate函数会销毁表,如果它存在,并重新创build它。 自动更新function改变表格。

在你的情况下,你需要在模式定义之后执行以下命令:

schema.autoupdate(); //或者automigrate

由于automigrate和autoupdate本质上是asynchronous的,因此在创build之前不应该访问表。 在这种情况下,您可以使用q模块或callback机制来解决这个问题。 下面是一个使用q模块解决scheme的代码片段:

(如果有帮助的话,请给它明星) https://gist.github.com/woonketwong/7619585

因此,向JugglingDB提交了一个请求。 该修补程序在迁移PostgreSQL模式(并在表中设置属性)中更新了其规范描述。 请求被接受并合并到主回购:

https://github.com/1602/jugglingdb/commit/5702d41e2cca2383715ad6b7263b25b7da2f181c