简体   繁体   English

Sequelize 多对多关系同一张表

[英]Sequelize many to many relationship same table

Hello you guys I have a question.大家好,我有一个问题。 Let's imagine we have social media.假设我们有社交媒体。

在此处输入图像描述

Let's imagine the users table looks like so.让我们想象一下 users 表看起来像这样。 and just like in any other social media, users can comment other users pics.就像在任何其他社交媒体中一样,用户可以评论其他用户的照片。 so we need a comments table that has a references to the user that posted the pic and the user that comments the pic.所以我们需要一个评论表,其中包含对发布图片的用户和评论图片的用户的引用。 在此处输入图像描述

So I need to findout a way to make a belongsToMany in sequelize .所以我需要找到一种在sequelize中制作belongsToMany的方法。

在此处输入图像描述

So far I have an accounts.ts file到目前为止,我有一个 accounts.ts 文件


import { AccountAttributes, Repository } from "@eneto/models";
import { DataTypes, Sequelize } from "sequelize";

/**
 * Account factory
 *
 * @param {import("sequelize").Sequelize} sequelize Sequelize database instance.
 * @returns {Repository<AccountAttributes>} Instance of Accounts Entity.
 */
function AccountFactory (sequelize: Sequelize): Repository<AccountAttributes> {
    return sequelize.define("accounts", {
        id: {
            type: DataTypes.BIGINT,
            allowNull: false,
            unique: true,
            primaryKey: true,
            autoIncrement: true,
        },
        username: {
            type: DataTypes.STRING,
            allowNull: false,
            unique: true,
        },
        psswrd: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        createdAt: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: DataTypes.NOW,
        },
        updatedAt: {
            type: DataTypes.DATE,
            allowNull: false,
            defaultValue: DataTypes.NOW,
        },
    }) as Repository<AccountAttributes>;
}

export { AccountFactory };

and on my index.ts I have it like:在我的 index.ts 上,我有它:

import { DB } from "@eneto/models";
import { Sequelize } from "sequelize";
import { env } from "../utils/env-vars";
import { AccountInfoFactory } from "./account-info";
import { AccountFactory } from "./accounts";

const sequelize = new Sequelize(env["ENETO_DB_NAME"], env["ENETO_DB_USER"], env["ENETO_DB_PASSWORD"], {
    port: env["ENETO_DB_PORT"],
    host: env["ENETO_DB_HOST"],
    dialect: "postgres",
    pool: {
        min: 0,
        max: 5,
        acquire: 30000,
        idle: 10000,
    },
});

const Accounts = AccountFactory(sequelize);
const AccountTypes = AccountTypesFactory(sequelize);
const AccountAddress = AddressFactory(sequelize);
const SocialMedia = SocialMediaFactory(sequelize);
const Educations = EducationFactory(sequelize);

AccountTypes.hasMany(Accounts);
AccountAddress.hasMany(Accounts);
Accounts.hasMany(SocialMedia);
Accounts.hasMany(Educations);
Accounts.hasMany(Experiences);
Accounts.hasMany(AccountInfo);
Accounts.hasMany(Media);

// I still dont know how to make a
Accounts.belongsToMany(Accounts);

export const db: DB = {
    Accounts,
    sequelize,
};

Found the answer:找到了答案:

Accounts.belongsToMany(Accounts,{ through: Comments,as: "to", foreignKey: "id" });
Accounts.belongsToMany(Accounts, { through: Comments, as: "from", foreignKey: "id" });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM