如何在 NestJs Sequlize 和 Mysql2 中添加多对多关联

问题描述 投票:0回答:0

很简单。我有博客模型、事件模型和标签模型。我想将博客与标签链接为多对多关系。我能够在数据库中设置 3 个表以及 blogtag 和 eventtag 映射表。但是我找不到向博客插入标签的方法(我可以创建博客但不能向 blogtag 表添加条目)。这是代码。我可以创建博客,但没有运气将它们附加到他们的标签

   import {
  Table,
  Column,
  Model,
  DataType,
  CreatedAt,
  UpdatedAt,
  DeletedAt,
  ForeignKey,
  BelongsTo,
  BelongsToMany
} from 'sequelize-typescript';
import { User } from '../../users/entities/user.entity';
import  {BlogTag, Tag} from '../../tags/entities/tag.entity'

@Table
export class Blog extends Model<Blog> {
  @Column({
    type: DataType.BIGINT,
    allowNull: false,
    autoIncrement: true,
    unique: true,
    primaryKey: true,
  })
  public id: number;

  @Column({
    type: DataType.STRING,
    allowNull: false,
  })
  title: string;

  @Column({
    type: DataType.STRING,
    allowNull: false,
  })
  blogData: string;

  @ForeignKey(() => User)
  @Column({
    type: DataType.BIGINT,
    allowNull: false,
  })
  userId: number;

  @BelongsTo(() => User)
  user: User;

  @Column({
    type: DataType.STRING,
    allowNull: true,
  })
  imageUrl: string;

  @Column({
    type: DataType.BOOLEAN,
    allowNull: false,
    defaultValue: false,
  })
  checked: boolean;

  @CreatedAt public createdAt: Date;

  @UpdatedAt public updatedAt: Date;

  @DeletedAt public deletedAt: Date;

  @BelongsToMany( ()=> Tag, ()=> BlogTag)
  tags: Tag[]
}

这是 blogs.ts

import {
    Table,
    Column,
    Model,
    DataType,
    CreatedAt,
    UpdatedAt,
    DeletedAt,
    ForeignKey,
    BelongsTo,
    BelongsToMany,
  } from 'sequelize-typescript';
  import { User } from '../../users/entities/user.entity';
  import { Event } from 'src/events/entities/event.entity';
  import { Blog } from 'src/blogs/entities/blog.entity';
  
  @Table
  export class Tag extends Model<Tag> {
    @Column({
      type: DataType.BIGINT,
      allowNull: false,
      autoIncrement: true,
      unique: true,
      primaryKey: true,
    })
    public id: number;
  
    @Column({
      type: DataType.STRING,
      allowNull: false,
    })
    name: string;

    @BelongsToMany( ()=> Blog, ()=> BlogTag)
    blogs: Blog[]
  
    @BelongsToMany( ()=> Event, ()=> EventTag)
    events: Event[]
  }
  
  @Table
  export class BlogTag extends Model<BlogTag>{
    @ForeignKey(() => Blog)
    @Column({
      type: DataType.BIGINT,
      allowNull: false,
    })
    blogId: number;

    @ForeignKey(() => Tag)
    @Column({
      type: DataType.BIGINT,
      allowNull: false,
    })
    tagId: number;
}

这是标签,这是我的博客 dto

export class CreateBlogDto {
  readonly title: string;
  readonly checked: boolean;
  readonly blogData: string;
  imageUrl: string;
  readonly userId: number;
  tags: number[];
}

这是我的服务

  async createBlog(createBlogDto: CreateBlogDto, blog_image) {
createBlogDto.imageUrl = blog_image.path;
// const tags = await this.tagService.getTagsByIds(createBlogDto.tags)
console.log(createBlogDto)
// console.log(tags)
// createBlogDto.tags = tags
return await this.blogRepository.create<Blog>(createBlogDto);

} 这是我的控制器。

      @UseGuards(AuthGuard('jwt'))
  @Post()
  create(
    @Body() createBlogDto: CreateBlogDto,
    @U[![**enter image description here**][1]][1]ploadedFile()
    blog_image: Express.Multer.File,
  ) {
    return this.blogsService.createBlog(createBlogDto, blog_image);

    
mysql orm sequelize.js nestjs many-to-many
© www.soinside.com 2019 - 2024. All rights reserved.