首页 > 解决方案 > 如何正确配置和计算 Sequelize 中的 belongsToMany 关联?

问题描述

我在 Sequelize 中遇到了这个 belongsToMany 的问题。

我正在创建一个视频内容网站。一个视频可以在多个列表中,显然,一个列表可以有多个视频(n:n)。一个视频只能属于一个频道 (1:n)。列表、视频和频道与帐户相关联(均为 1:n)。

我想获取所有列表以及每个列表中的视频数量。

所以我以这种方式创建了模型:

视频:

export class Video extends Model {
  public static associations: {
    channel: BelongsTo;
    account: BelongsTo;
    list: BelongToMany;
  };

  public id: string;
  public title: string;
  public desc: string;
  public createdAt: Date;
  public updatedAt: Date;

  public channelId: string;
  public channel: Channel;
  public getChannel: BelongsToGetAssociationMixin<Channel>;
  public setChannel: BelongsToSetAssociationMixin<Channel, string>;

  public accountId: string;
  public account: Conta;
  public getAccount: BelongsToGetAssociationMixin<Account>;
  public setAccount: BelongsToSetAssociationMixin<Account, string>;
}

Video.init(
  {
    title: STRING(100),
    descricao: STRING(500),
  },
  {
    sequelize,
    modelName: 'videos',
    hooks: {
      beforeCreate: (model, options) => {
        model.id = uuidv4();
      }
    }
  }
);

import { Channel } from './channels';
export const channel = Video.belongsTo(Channel, { foreignKey: 'channelId' });

import { Account } from './accounts';
export const account = Video.belongsTo(Account, { foreignKey: 'accountId' });

import { List } from './lists';
export const lists = Video.belongsToMany(List, {
  foreignKey: 'videoId' ,
  through: 'videos_list',
  timestamps: false
});

列表:

export class List extends Model {
  public static associations: {
    account: BelongsTo;
    videos: BelongsToMany;
  };

  public id: string;
  public name: string;
  public desc: string;
  public createdAt: Date;
  public updatedAt: Date;

  public accountId: string;
  public account: Conta;
  public getAccount: BelongsToGetAssociationMixin<Account>;
  public setAccount: BelongsToSetAssociationMixin<Account, string>;

  public videos: Video[];
  public getVideos: BelongsToManyGetAssociationsMixin<Video>;
  public setVideos: BelongsToManySetAssociationsMixin<Video, string>;
  public addVideo: BelongsToManyAddAssociationMixin<Video, string>;
  public addVideos: BelongsToManyAddAssociationsMixin<Video, string>;
  public createVideo: BelongsToManyCreateAssociationMixin<string>;
  public countVideos: BelongsToManyCountAssociationsMixin;
  public hasVideo: BelongsToManyHasAssociationMixin<Video, string>;
  public removeVideo: BelongsToManyRemoveAssociationMixin<Video, string>;
  public removeVideos: BelongsToManyRemoveAssociationsMixin<Video, string>;
}

Lista.init(
  {
    name: STRING(100),
    desc: STRING(500),
  },
  {
    sequelize,
    modelName: 'lists',
    hooks: {
      beforeCreate: (model, options) => {
        model.id = uuidv4();
      }
    }
  }
);

import { Account } from './accounts';   
export const account = Lista.belongsTo(Account, {
  foreignKey: 'accountId',
  as: 'account'
});

import { Video } from './videos';
export const videos = List.belongsToMany(Video, {
  foreignKey: 'listId',
  through: 'videos_list',
  timestamps: false
});

我正在做这样的查询:

  List.findAll({
    attributes: [
      'name', 'desc',
      [ Sequelize.fn("COUNT", Sequelize.col("videos.id")), "countVideos" ]
    ],
    include: [{ model: Video, as: 'videos', attributes: [] }],
    group: ['videos.listId'],
    order: [['name', 'DESC']]
  })

但我收到以下错误:

DatabaseError [SequelizeDatabaseError]: column videos.listId does not exist

我在这里做错了什么?

我只列出了我认为相关的列表和视频模型。如果您需要其他任何东西,请告诉我,我会包括在内。

标签: node.jspostgresqlsequelize.js

解决方案


List.belongsToMany(Video...你有videoIdFK 而不是listId.


推荐阅读