当前位置: 首页 > 知识库问答 >
问题:

前端 - nestjs+typeorm,多对多关系,删除报错?

向杜吟
2023-07-10

文章实体

import {
  Column,
  Entity,
  JoinTable,
  ManyToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { Tag } from './tag.mysql.entity';

@Entity()
export class Article {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({
    length: 100,
    comment: '文章标题',
  })
  title: string;

  @Column({
    type: 'text',
    comment: '文章内容',
  })
  content: string;

  @JoinTable()
  @ManyToMany(() => Tag, (tag) => tag.articles)
  tags: Tag[];
}

标签实体

import {
  Column,
  Entity,
  JoinTable,
  ManyToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { Article } from './article..mysql.entity';

@Entity()
export class Tag {
  @PrimaryGeneratedColumn()
  id: number;
  @Column({
    length: 50,
    comment: '标签名',
  })
  name: string;

  @ManyToMany(() => Article, (article) => article.tags)
  articles: Article[];
}

然后我测试新增,随便添加几条数据

// 测试多对多的关系 新增
  async textManytoMany() {
    const t1 = new Tag();
    t1.name = '测试标签1_' + this.random();

    const t2 = new Tag();
    t2.name = '测试标签2_' + this.random();
    const t3 = new Tag();
    t3.name = '测试标签3_' + this.random();
    const a1 = new Article();
    a1.title = '测试文章1_' + this.random();
    a1.content = '测试文章1_' + this.random();
    a1.tags = [t1, t2, t3];
    const a2 = new Article();
    a2.title = '测试文章2_' + this.random();
    a2.content = '测试文章2_' + this.random();
    a2.tags = [t1, t2, t3];
    const a3 = new Article();
    a3.title = '测试文章3_' + this.random();
    a3.content = '测试文章3_' + this.random();
    a3.tags = [t1, t2, t3];
    await this.tagRepository.save([t1, t2, t3]);
    await this.articleRepository.save([a1, a2, a3]);
    return {
      t1,
      t2,
      t3,
      a1,
      a2,
      a3,
    };
  }

查询文章id=1
image.png
查询标签id=6
image.png
删除文章没问题
image.png
但是,删除标签id=6就报错了。服务器错误
image.png


  // 测试多对多的关系 查询文章
  async findArticle(id: number) {
    const article = await this.articleRepository.findOne({
      where: { id },
      relations: ['tags'], // 这里的 tags 是 article.entity.ts 中的 tags 字段 @ManyToMany(() => Tag)
    });
    return article;
  }

  // 测试多对多的关系 查询标签
  async findTag(id: number) {
    const tag = await this.tagRepository.findOne({
      where: { id },
      relations: ['articles'], // 这里的 articles 是 tag.entity.ts 中的 articles 字段 @ManyToMany(() => Article)
    });
    return tag;
  }

  // 测试多对多的关系 删除文章
  async deleteArticle(id: number) {
    return this.articleRepository.delete(id);
  }

  // 测试多对多的关系 删除标签
  async deleteTag(id: number) {
    return this.tagRepository.delete(id);
  }

query failed: DELETE FROM `tag` WHERE `id` IN (?) -- PARAMETERS: [6]
error: Error: Cannot delete or update a parent row: a foreign key constraint fails (`typeorm_test`.`article_tags_tag`, CONSTRAINT `FK_5fee2a10f8d6688bd2f2c50f15e` FOREIGN KEY (`tagId`) REFERENCES `tag` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)

共有1个答案

施同
2023-07-10

article没有写manytomany映射关系

 类似资料:
  • 问题内容: 我要删除具有多对多关系的一个站点上的表中的一行。我还想删除该关系另一侧的所有相关行。 例如,假设我有以下表格,并且想从中删除一行。我也想从中删除所有相关的行,当然,也删除其中不再需要的任何行。 我知道如何在查询中连接以上表格。但是我看不到如何删除整个关系中的数据。 注意:关系的两端都实现级联删除。因此,例如,从中删除行将删除中的任何相关行。但是显然,这不会传播到表中。 问题答案: 我认

  • 我正试图实现与自定义字段的多对多关系,就像下面演示的那样。我的数据与他给出的例子不同,但是关系的结构是相同的。我能够成功地创建关系,但我有一个问题,即产品(product_id)的外键为空,即使客户端正在为该字段发送数据。出于某种原因,TypeORM正在删除它并输入空值。 以下是连接表的结构: 我的服务通过以下功能提交数据: 提交表单时,装运数据将与shipmentProducts一起成功保存,但

  • 我目前正在开发一个网络票证系统,并有课程来存储我的票证数据。每个票据都可以有多个相关标签,为了管理这些标签,我创建了一个标签编辑器。这很好,只缺少一个删除选项。到目前为止,大多数删除都失败了,原因是该标签仍然被另一个票证引用,这需要先删除它。在寻找解决方案的过程中,我遇到了CascadeType。删除,这似乎正是我想要的。 但是,由于ticket对象包含一组标签,而不是相反,因此每次我删除一张ti

  • 问题内容: 我有两个与多对多关联的表。 —数据库片段: 加载 ID 名称 会话 ID 日期 sessionsloads LoadId 的SessionID —hibernate映射片段: 为了从关联表 sessionloads中 删除一个条目,我执行以下代码: 但是,启动后,此代码将保持不变。 删除关联的正确方法是什么? 问题答案: 您需要更新和之间的链接的两端: 实际上,许多开发人员使用防御性方

  • 我正在使用hibernate多对多的关联。我有3个表(STUDENT,COURSE和STUDENT_COURSE)。在3个表中,2个是主表,1个是提供关系的中间表。当记录从STUDENT中删除时,相应的映射将从Student_Course中删除。我的要求是它甚至应该从课程表中删除记录。考虑以下STUDENT_COURSE条目: 当从学生表中删除101时,上述表中的第一个条目被删除,但课程表中对应于

  • 在我的项目中,我使用Spring数据jpa。我有多对多关系的表格。我的实体: 和零件: 现在在Controller中,我尝试从表部分中删除一行: 但我有例外: 区分完整性约束冲突:“FK9Y4MKICYBLJWPENACP4298I49:PUBLIC.PARTS外键(ID\u导出)引用PUBLIC.EXPORT(ID)(1)”;SQL语句:/*删除com.aleksandr0412.demo.en