简体   繁体   中英

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. 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 .

在此处输入图像描述

So far I have an accounts.ts file


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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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