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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
遇到跟你一模一样的问题