8

I have relations:

(Track) -- M:1 --> (TrackChannel) <--1:M (Channel)
(User) -- M:1 --> (UserChannel) <--1:M (Channel)

  • Channel model include object Track as current_track_id with relation one to one.
  • Track and Channel is related many to many through TrackChannel
  • User and Channel is related many to many through UserChannel

/* Channel.js: */

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('channel', {
    id: {
      allowNull: false,
      primaryKey: true,
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
    },
    current_counter: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0
    },
    track_counter: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0
    },
    track_id: {
      type: DataTypes.STRING,
      allowNull: true,
      references: {
        model: 'track',
        key: 'id'
      }
    },
    createdAt: {
      type: DataTypes.TIME,
      allowNull: true,
      defaultValue: sequelize.fn('now')
    },
    updatedAt: {
      type: DataTypes.TIME,
      allowNull: true,
      defaultValue: sequelize.fn('now')
    }
  }, {
    tableName: 'channel',
    classMethods:{
      associate:function(models){
        this.belongsToMany(models.user, { onDelete: "CASCADE", foreignKey: 'user_id', otherKey: 'channel_id', through: 'userChannel' })
        this.belongsToMany(models.track, { onDelete: "CASCADE", foreignKey: 'track_id', otherKey: 'channel_id', through: 'trackChannel' })
        this.belongsTo(models.track, {foreignKey: 'current_track_id' , foreignKeyConstraint: true})
      }
    }
  });
};

What you are doing?

That is my query for the channel. I use repository pattern:

return db.channel.findOne({
            raw:true,
            include: [
                { model: db.track, attributes: ['id', 'name','artist_name' ,'album_name'], where: {track_id, }, paranoid: true, required: false}
            ],
            where: {
                id: id
            }
        });

What do you expect to happen?

I want to get:

{
  "id": "ce183d0a-e702-49a3-83b5-2912bbcf5283",
  "current_counter": 0,
  "track_counter": 0,
  "current_track_id": {} // object or null,
  "createdAt": "21:26:56.487217",
  "updatedAt": "21:26:56.487217",
  "tracks: [] // array or null
}

What is actually happening?

I try to create query to get one channel where is included current track object and list of track. Now it looks like that:

{
  "id": "ce183d0a-e702-49a3-83b5-2912bbcf5283",
  "current_counter": 0,
  "track_counter": 0,
  "track_id": null,
  "createdAt": "21:26:56.487217",
  "updatedAt": "21:26:56.487217",
  "tracks.id": null,
  "tracks.name": null,
  "tracks.artist_name": null,
  "tracks.album_name": null,
  "tracks.trackChannel.id": null,
  "tracks.trackChannel.channel_id": null,
  "tracks.trackChannel.createdAt": null,
  "tracks.trackChannel.updatedAt": null,
  "tracks.trackChannel.track_id": null
}

Dialect: postgres Database version: 9.6 Sequelize version: 3.3.0

4
  • github.com/sequelize/sequelize/issues/4973 - that is solution to get track as array. But how to get related tracks Commented Feb 4, 2017 at 22:08
  • In case you get completely stuck with the sequelize, there is pg-promise, which perfectly supports the repository pattern, as shown in pg-promise-demo, which can happily coexist with your sequelize code ;) Commented Feb 4, 2017 at 23:25
  • Thx I will take a look. But it does help me clean my code :) Commented Feb 4, 2017 at 23:43
  • 1
    You need to set nest: true github.com/sequelize/sequelize/issues/… Commented Feb 19, 2021 at 17:59

2 Answers 2

8

Guess it'll help someone so giving another option.

You can use nest:true along with raw:true property given by sequelize.

result:

before:

{
        id: 1,
        createdBy: 1,
        'Section.id': 1,
        'Section.name': 'Breakfast',
}

After:

{
  id: 1,
  createdBy: 1,
  Section: {
      id: 1,
      name: ''
      }
}
Sign up to request clarification or add additional context in comments.

Comments

4

Solution:

findById: function(id) {
        return db.channel.findOne({
            logging: true,
            include: [
                { model: db.track, attributes: ['id', 'name','artist_name' ,'album_name'], as: 'track'},
                { model: db.track, attributes: ['id', 'name','artist_name' ,'album_name'], as: 'tracks', paranoid: true, required: false}
            ],
            where: {
                id: id
            }
        });
    },

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.