TypeORM的OneToMany关系在创建时未将条目添加到数组中。

huangapple go评论58阅读模式
英文:

TypeORM OneToMany relation doesn't add entry to the array when it is created

问题

I am trying to create two entities - Assignment and Lesson. Each Lesson can have multiple Assignments and each Assignment can be part of one Lesson. When I create a Lesson with Assignments in it, the Assignments get created, they are added to the assignments array, and everything works.

When I create a Lesson and then try to add a new Assignment to it, the Assignment is created, it is linked to the correct Lesson, but the assignments field in the Lesson is not updated.

Is this how it is supposed to work, or is there any way to update the assignments array in Lesson?

Below is my code. I am using an in-memory SQLite database.

Assignment.ts

import { Entity, Column, PrimaryGeneratedColumn, TableInheritance, ManyToOne } from 'typeorm';
import { Lesson } from './Lesson';

@Entity()
@TableInheritance({ column: { type: 'varchar', name: 'type' } })
export abstract class Assignment {
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    name: string;

    @ManyToOne(() => Lesson, (lesson) => lesson.assignments, { eager: true })
    lesson: Lesson;
}

Lesson.ts

import { Entity, Column, PrimaryGeneratedColumn, OneToMany } from 'typeorm';
import { Assignment } from './Assignment';

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

    @Column()
    name: string;

    @OneToMany(() => Assignment, (assignment) => assignment.lesson, { cascade: true })
    assignments: Assignment[];
}

I have tried adding @JoinColumn() to the @ManyToOne relation, but nothing changed.

英文:

I am trying to create two entities - Assignment and Lesson. Each Lesson can have multiple Assignments and each Assignment can be part of one Lesson. When I create a Lesson with Assignments in it, the Assignemnts get created, they are added to the assignments array and everything works.

When I create a Lesson and then try to add a new Assignment to it, the Assignment is created, it is linked to the correct Lesson but the assignments field in the Lesson is not updated.

Is this how it is supposed to work or is there any way to update the assignments array in Lesson?

Below is my code. I am using a sqlite in-memory database

Assignment.ts

import { Entity, Column, PrimaryGeneratedColumn, TableInheritance, ManyToOne, JoinColumn } from 'typeorm';
import { Lesson } from './Lesson';

@Entity()
@TableInheritance({ column: { type: 'varchar', name: 'type' } })
export abstract class Assignment {
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    name: string;

    @ManyToOne(() => Lesson, (lesson) => lesson.assignments, { eager: true })
    @Column('json')
    lesson: Lesson;
}

Lesson.ts

import { Entity, Column, PrimaryGeneratedColumn, OneToMany } from 'typeorm';
import { Assignment } from './Assignment';

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

    @Column()
    name: string;

    @OneToMany(() => Assignment, (assignment) => assignment.lesson, { cascade: true })
    @Column('json')
    assignments: Assignment[];
}

I have tried adding @JoinColumn() to the @ManyToOne relation but nothing changed

答案1

得分: 0

你可能需要添加显示如何访问数据的代码...但我会试一试:

通常,你会像这样将一个课程添加到一个作业中:

const lesson = await Lesson.findOne({ where: { id: 1 }, relations: ['assignments'] });
const assignment = await Assignment.findOne({ where: { id: 1 } });
assignment.lesson = lesson;
await assignment.save();
// 在这一点上,课程不会包含你的新作业
// 你可以将它添加到 lesson.assignments 数组中
lesson.assignments.push(assignment);
// 或者你可以重新从数据库中读取
const lessonAfterAdd = await Lesson.findOne({ where: { id: 1 }, relations: ['assignments'] });

请注意,以上代码是基于提供的代码片段进行翻译的。

英文:

You should probably add your code that shows how you are accessing your data... but Ill take a stab:

typically you'll add a lesson to an assignment as follows:

const lesson = await Lesson.findOne({where:{id:1},relations:['assignments]})
const assignment = await Assignment.findOne({where:{id:1}}
assignment.lesson = assignment
await assignment.save()
// at this point lesson wont have your new assignment
// you could just add it to the lesson.assignments array
lesson.assignments.push(assignment)
// or you could re-read from the db
const lessonAfterAdd = 
await Lesson.findOne({where:{id:1},relations['assignments]})

答案2

得分: 0

The solution was to remove the @Column annotation from both of the fields - assignments and lesson.

I could also add a filed lessonId: number with the @Column annotation to store the lesson id in the Assignment.

英文:

The solution was to remove the @Column annotation from both of the fields - assignments and lesson.

I could also add a filed lessonId: number with the @Column annotation to store the lesson id in the Assignment.

huangapple
  • 本文由 发表于 2023年5月6日 18:19:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定