Wednesday 26 July 2017

Express and Sequelize many-to-many

Hello, i just started working with Sequelize, and have to say I feel the docs are a bit incomplete could just be me. I wanna create a many to manyAnd the user has many tickets and a ticket has many users.I have used the sequelize-cli to generate the migration and model for User and ticket, and they look like this:'use strict'; module.exports = function(sequelize, DataTypes) { var Ticket = sequelize.define('Ticket', { title: DataTypes.STRING, description: DataTypes.TEXT, status: DataTypes.TEXT, type: DataTypes.TEXT, priority: DataTypes.TEXT, created_at: DataTypes.DATE, updated_at: DataTypes.DATE }, { underscored: true, classMethods: { associate: function(models) { Ticket.belongsToMany(User, { thorough: UserTicket }) } } }); return Ticket; };'use strict'; module.exports = function(sequelize, DataTypes) { var User = sequelize.define('User', { first_name: DataTypes.STRING, last_name: DataTypes.STRING, email: DataTypes.STRING, password: DataTypes.STRING, created_at: DataTypes.DATE, updated_at: DataTypes.DATE }, { underscored: true, classMethods: { associate: function(models) { User.belongsToMany(Ticket, { thorough: UserTicket}) User.belongsTo(Group) } } }); return User; };I have than created a migration for UserTicket manually which contains a table that has a user_id, and ticket_id. I try to get the all users in a ticket by doingrouter.get('/:id', auth.authenticate(), function(req, res, next) { Ticket.find({where: {id: req.params.id}}, { include : [ { model: all, nested: true }] }).then(ticket => { return res.json(ticket) }).catch(err => { console.log(err) }) });The error in my console is just GET /api/ticket/1 500 100.092 ms - 427 Executing (default): SELECT id, first_name, last_name, email, password, created_at, updated_at FROM Users AS User WHERE User.id = 1;Nothing else Hope someone can guide me a bit here.

Submitted July 26, 2017 at 03:28PM by cawex

No comments:

Post a Comment