Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var User = db.seq.define('User',{
- username: { type: db.Sequelize.STRING},
- email: { type: db.Sequelize.STRING},
- password: { type: db.Sequelize.STRING},
- sex : { type: db.Sequelize.INTEGER},
- day_birth: { type: db.Sequelize.INTEGER},
- month_birth: { type: db.Sequelize.INTEGER},
- year_birth: { type: db.Sequelize.INTEGER}
- });
- User.sync().success(function(){
- console.log("table created")
- }).error(function(error){
- console.log(err);
- })
- var Post = db.seq.define("Post",{
- body: { type: db.Sequelize.TEXT },
- user_id: { type: db.Sequelize.INTEGER},
- likes: { type: db.Sequelize.INTEGER, defaultValue: 0 },
- });
- Post.sync().success(function(){
- console.log("table created")
- }).error(function(error){
- console.log(err);
- })
- db.seq.query('SELECT * FROM posts, users WHERE posts.user_id = users.id ').success(function(rows){
- res.json(rows);
- });
- User.hasMany(Post, {foreignKey: 'user_id'})
- Post.belongsTo(User, {foreignKey: 'user_id'})
- Post.find({ where: { ...}, include: [User]})
- SELECT
- `posts`.*,
- `users`.`username` AS `users.username`, `users`.`email` AS `users.email`,
- `users`.`password` AS `users.password`, `users`.`sex` AS `users.sex`,
- `users`.`day_birth` AS `users.day_birth`,
- `users`.`month_birth` AS `users.month_birth`,
- `users`.`year_birth` AS `users.year_birth`, `users`.`id` AS `users.id`,
- `users`.`createdAt` AS `users.createdAt`,
- `users`.`updatedAt` AS `users.updatedAt`
- FROM `posts`
- LEFT OUTER JOIN `users` AS `users` ON `users`.`id` = `posts`.`user_id`;
- Model1.belongsTo(Model2, { as: 'alias' })
- Model1.findAll({include: [{model: Model2 , as: 'alias' }]},{raw: true}).success(onSuccess).error(onError);
Add Comment
Please, Sign In to add comment