У меня проблемы, это относится ко многим вещам в Sequelize.
Я создаю сайт с видеоконтентом. Одно видео может быть во многих списках и, очевидно, в списке может быть много видео (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
Что я тут не так делаю?
Я перечислил только те модели Список и Видео, которые, на мой взгляд, были актуальны. Если вам понадобится что-нибудь другое, пожалуйста, дайте мне знать, и я включу его.