Skip to content

Instantly share code, notes, and snippets.

@sdepold
Created February 7, 2012 06:34
Show Gist options
  • Save sdepold/1757695 to your computer and use it in GitHub Desktop.
Save sdepold/1757695 to your computer and use it in GitHub Desktop.
hasMany + belongsTo in sequelize
var Sequelize = require("sequelize")
var sequelize = new Sequelize('database', 'root')
var User = sequelize.define('User', { username: Sequelize.STRING })
var Comment = sequelize.define('Comment', { text: Sequelize.TEXT })
User.hasMany(Comment)
Comment.belongsTo(User)
sequelize.sync({force: true}).success(function() {
User.create({ username: 'sdepold' }).success(function(sdepold) {
Comment.create({ text: 'woohoo nice woohoo' }).success(function(comment) {
sdepold.setComments([comment]).success(function() {
sdepold.getComments().success(function(comments) {
console.log(comments)
})
})
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment