I am using Node with Express, and am moving my ORM from Mongoose (Mongo) to JugglingDB (Postgres), and am having a tough time getting JugglingDB to use a simple schema that I defined.
My schema is as follows:
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();
}
});
I get the following error when trying to start up node:
{ [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' }
Can you please let me know what I am missing? Thanks for any help!
For any PostgreSQL schema defined through JugglingDB, it needs to be automigrated or autoupdated. The automigrate function destroys the table if it exists, and re-create it. The autoupdate function alters the table.
In your case, you will want to execute the following after your schema define:
schema.autoupdate(); // or automigrate
Since automigrate and autoupdate are async in nature, one shall not access the table before it is created. In this case, you can use the q module or callback mechanism to tackle this problem. A code snippet using the q module solution is illustrated at:
(Please star it if it helps :) ) https://gist.github.com/woonketwong/7619585
Consequently, a pull request was submitted to JugglingDB. The fix updated its spec description in migrating PostgreSQL schema (and setting attributes in table). The request was accepted and merged to the master repo at:
https://github.com/1602/jugglingdb/commit/5702d41e2cca2383715ad6b7263b25b7da2f181c