How to do a sequelize association model BelongsToMany?

316 Views Asked by At

I am trying to associate in the middle table two models with M:N (User.ts and List.ts). In this case I am using typescript-sequelize package to make the models and in the documentation for the package it sais that i can use the .create method in the table as in sequelize.

I do the method create on the List and give two parameters, the first it is all the information to add to the table and the second it should be the association with the other list. But i read the documentation for the method and i didnt find the syntax to it.

https://sequelize.org/docs/v6/advanced-association-concepts/creating-with-associations/ https://www.npmjs.com/package/sequelize-typescript#multiple-relations-of-same-models

This is RouteList.ts:

import express from "express";
import { List } from "../models/List";
import { User } from "../models/User";


router.post("/", async (req, res) => {
 const { title, list, tags } = req.body;
 const lists = await List.findAll();

 if (!title || !list || !tags) {
   return res.status(400).json("Missing values");
 } else {
   req.body.id = lists.length + 1;

   await List.create(req.body, { include: [User] });
   return res.status(201).json("The list was succesfully created");
 }
});

This is List.ts (model)

import {
  Table,
  Column,
  Model,
  PrimaryKey,
  BelongsToMany,
  DataType,
} from "sequelize-typescript";
import { User } from "./User";
import { UserList } from "./UserList";

@Table
export class List extends Model<List> {
  @PrimaryKey
  @Column
  id!: number;

  @Column
  title!: string;

  list!: {
    enumList: string[];
  };

  tags!: {
    enumTags: string[];
  };

  @Column({
    type: DataType.ARRAY(DataType.JSON),
    defaultValue: [],
  })
  comments!: {
    idUserComent: number;
    userComent: string;
  }[];

  @BelongsToMany(() => User, () => UserList)
  users!: User[];
}

this is User.ts (model)

import {
  Table,
  Column,
  Model,
  PrimaryKey,
  Default,
  Unique,
  HasMany,
  BelongsToMany,
  HasOne,
} from "sequelize-typescript";
import { Note } from "./Note";
import { UserList } from "./UserList";
import { Role } from "../types";
import Calendar from "./Calendar";

@Table
export class User extends Model<User> {
  @PrimaryKey
  @Column
  id!: number;

  @Column
  nickname!: string;

  @Unique
  @Column
  email!: string;

  @Default(false)
  @Column
  verifiedEmail!: boolean;

  @Column
  password!: string;

  @Column
  role!: Role;

  @Column
  avatar!: string;

  @HasMany(() => Note)
  notes!: Note[];

  @HasOne(() => Calendar)
  calendar!: Calendar;

  @BelongsToMany(() => User, () => UserList)
  users!: User[];
}

My question is, how do i tell sequelize to make the middle table with the id of a particular user? Do i have to do it manualy with another .create on the middletable UserList.ts?

I tried to add the id of the user everywhere and the results are either wrong syntax or the tables work fine but the middletable don't.

Github repo: https://github.com/Koppeks/Api-ts

1

There are 1 best solutions below

0
Hai Nguyen On

I think you have to create a new Model UserList to associate between model User and model List