Created
February 7, 2012 06:34
-
-
Save sdepold/1757695 to your computer and use it in GitHub Desktop.
hasMany + belongsTo in sequelize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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