Try to create HABTM connection Sequelize.js

I'm trying to create a HABTM relationship with Sequelize but I can't get it done.... I still receive an error message:

return (tableName1.toLowerCase() < tableName2.toLowerCase()) ? (tableName1 ^ TypeError: Cannot call method 'toLowerCase' of undefined

I have a User model, a Book model and an UserBooks model. And ofcourse my database contains a "users" table, "user_books" table and "books" table.

UserBooks model:

module.exports = function(schema, DataTypes) { var UserBooks = schema.define('UserBooks', { }, { tableName: 'user_books', // this will define the table's name timestamps: false // this will deactivate the timestamp columns }); UserBooks.sync(); return UserBooks; };

User model:

module.exports = function(schema, DataTypes) { var User = schema.define('User', { keywords: DataTypes.STRING }, { tableName: 'users', // this will define the table's name timestamps: false ,// this will deactivate the timestamp columns syncOnAssociation:false }); User.hasMany(Book, { foreignKey: 'user_id', through: UserBooks }); User.sync(); return User; };

Book model:

module.exports = function(schema, DataTypes) { var Book = schema.define('Book', { keywords: DataTypes.STRING }, { tableName: 'books', // this will define the table's name timestamps: false ,// this will deactivate the timestamp columns syncOnAssociation:false }); Book.hasMany(User, { foreignKey: 'book_id', through: UserBooks }); Book.sync(); return Book; };

-------------Problems Reply------------

In your User model you are trying to create an association with a model that is not defined in that scope. In User.js, you only have access to User, not Book or UserBooks which are undefined. Thats whats causing your error.

You can either create associations in the place where you import all your models into your app, or in the models file by importing the models you want to associate with (bevare of circular imports). Your user model could be changed to:

module.exports = function(schema, DataTypes) {
var Book = schema.import(__dirname + '/book');
var UserBooks = schema.import(__dirname + '/userbooks');
var User = schema.define('User', {
keywords: DataTypes.STRING
}, {
tableName: 'users', // this will define the table's name
timestamps: false ,// this will deactivate the timestamp columns
syncOnAssociation:false
});

User.hasMany(Book, { foreignKey: 'user_id', through: UserBooks });
Book.hasMany(User, { foreignKey: 'book_id', through: UserBooks });

return User;
};

For another example of how to do it, see http://sequelizejs.com/articles/express#minimal-express-app

Also, I've removed the call to User.sync from your code. Sync is an async call, while import is sync. This means that your are defining your model, starting to sync it to the DB, and then returning it, before you know that it has finished syncing. This means you could potentially be trying to work create instances with it before the table has been created. Instead, you should use sequelize.sync to sync all your models at once, and attach a callback to wait for the sync to finish (see the link I posted for a code example)

Category:node.js Views:0 Time:2014-05-09

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.151 (s). 11 q(s)