Skip to content

Instantly share code, notes, and snippets.

@mickhansen
Created December 15, 2013 17:26
Show Gist options
  • Save mickhansen/7975651 to your computer and use it in GitHub Desktop.
Save mickhansen/7975651 to your computer and use it in GitHub Desktop.
var Sequelize = require('sequelize'),
sequelize = new Sequelize('sequelize_test', 'sequelize_test', 'YDLvjZJv9GdvbM8G', {
syncOnAssociation: false,
logging: console.log
});
var User = sequelize.define('User', {}),
Post = sequelize.define('Post', {}),
SharedPosts = sequelize.define('SharedPosts', {})
Post.belongsTo(User, {as: 'Creator', foreignKey: 'creator_id', foreignKeyConstraint: true});
User.hasMany(Post, {foreignKey: 'creator_id'});
User.hasMany(Post, {as: 'SharedPosts', joinTableModel: SharedPosts, foreignKey: 'user_id'}); // , foreignKey: 'user_id', foreignKeyConstraint: true
Post.hasMany(User, {as: 'SharedWithUsers', joinTableModel: SharedPosts, foreignKey: 'post_id'}); // , foreignKey: 'post_id', foreignKeyConstraint: true
sequelize.sync({force: true}).done(function (err) {
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment