1

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!

Scott Switzer
  • 1,064
  • 1
  • 15
  • 25

2 Answers2

2

Specify the tablename using the table property, like so:

var Organization = schema.define('Organization', {
  name: String,
  slug: String,
  link: String
}, {
  table: 'organizations'
});
Peter Marklund
  • 1,033
  • 10
  • 9
  • Hi Peter - thanks for the suggestion. I did this in my definitions for User and UserToken above. I have given up JuggleDB - it is not stable enough. I went to Sequelize instead. Thanks anyway! – Scott Switzer Nov 05 '13 at 23:40
2

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

Woon Ket
  • 31
  • 3