• 0
  • 新人请关照

typeorm关联更新出错

使用typeorm创建多对多关联,进行更新从表数据时出错

相关代码

blog.entity
import { Entity , Column ,PrimaryGeneratedColumn,CreateDateColumn, UpdateDateColumn, JoinColumn, ManyToOne, ManyToMany, JoinTable, } from "typeorm";
import { ApiProperty } from "@nestjs/swagger";

import { UserEntity } from "./../user/user.entity";
import {TagsEntity} from './../tag/tag.entity'

@Entity({name:'tyk_blog'})
export class BlogEntity {
    @ApiProperty({description:'主键'})
    @PrimaryGeneratedColumn({name:'blog_id'})
    blogId:number

    @ApiProperty({description:'标题'})
    @Column()
    title:string

    @ApiProperty({description:'存储内容'})
    @Column({type:'text'})
    content:string
    
    @ApiProperty({description:'页面展示内容'})
    @Column({name:'html_content',type:'text'})
    htmlContent:string

    @ApiProperty({description:'是否发布,默认不发布'})
    @Column({name:'is_release',default:false})
    isRelease:boolean


    @ApiProperty({description:'创建时间'})
    @CreateDateColumn({ type: 'timestamp', name: 'create_date', comment: '创建时间' })
    createDate: Date

    @ApiProperty({description:'更新时间'})
    @UpdateDateColumn({ type: 'timestamp', name: 'update_date', comment: '更新时间' })
    updateDate: Date

    @Column({name:'user_id'})
    userId:number

    @ManyToOne((type)=>UserEntity,(user)=>user.blogs)
    @JoinColumn({name:'user_id'})
    user:UserEntity

    @ManyToMany((type)=>TagsEntity,(tag)=> tag.blogs)
    @JoinTable({
        name: 'tyk_blog_tag',
        joinColumns: [
          {name: 'blog_id'},
        ],
        inverseJoinColumns: [
          {name: 'tag_id'},
        ],
      })
    tags:TagsEntity[]
}
tag.entity
import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, JoinColumn, OneToMany, ManyToMany, JoinTable } from "typeorm";

import {BlogEntity} from '../blog/blog.entity'

@Entity({name:'tyk_tag'})
export class TagsEntity{
    @PrimaryGeneratedColumn({name:'tag_id'})
    tagId:number

    @Column()
    name:string

    @ManyToMany(
        type=>BlogEntity,
        blog=>blog.tags
    )
    blogs:BlogEntity[]
}
出错部分
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";

import {BlogEntity} from './blog.entity'
import {TagService} from './../tag/tag.service'
import { Repository } from "typeorm";
import { UserService } from "../user/user.service";

@Injectable()
export class BlogService{
    constructor(
        @InjectRepository(BlogEntity)
        private readonly blogService:Repository<BlogEntity>,

        private tagsService:TagService,
        private userService:UserService,
    ){}
    async update(blog){
        try {
            let tagsList=await   this.tagsService.findByIds(blog.tags);
            blog.tags=tagsList;
            return await this.blogService.update(7,{
                tags:tagsList,
            })
        } catch (error) {
            console.log(error)
        }
        
    }
}
打印出错error
QQ图片20200603094819.png
阅读 385
评论
    0 个回答
    撰写回答

    登录后参与交流、获取后续更新提醒

    相似问题
    推荐文章