【问题标题】:Sequelize include array of nested object - node.jsSequelize 包含嵌套对象数组 - node.js
【发布时间】:2017-06-22 02:41:13
【问题描述】:

我有关系:

(Track) -- M:1 --> (TrackChannel) (用户) -- M:1 --> (用户频道)

  • Channel 模型包含对象 Trackcurrent_track_id 并具有一对一的关系。
  • TrackChannel 通过 TrackChannel 多对多相关联
  • UserChannel 通过 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})
      }
    }
  });
};

你在做什么?

这是我对频道的查询。我使用存储库模式:

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
            }
        });

您预计会发生什么?

我想得到:

{
  "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
}

实际发生了什么?

我尝试创建查询以获取一个频道,其中包含当前曲目对象和曲目列表。现在看起来像这样:

{
  "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
}

方言:postgres 数据库版本:9.6 续集版本:3.3.0

【问题讨论】:

标签: node.js postgresql sequelize.js


【解决方案1】:

解决方案:

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
            }
        });
    },

【讨论】:

    【解决方案2】:

    猜猜它会帮助某人,所以给另一个选择。

    您可以将nest:true 与sequ​​elize 提供的raw:true 属性一起使用。

    结果:

    之前:

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

    之后:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-27
      • 2021-02-09
      • 2021-05-01
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      相关资源
      最近更新 更多