1

I got two entities with some relationships going on. The problem is that the EntityManager only saves the main entity and sets the foreign keys to NULL. I have read a similar question that OP seems to have solved himself, but his answer leaves me clueless.

I'm used to ORMs being able to insert nested entities in one call. Does have TypeORM have such functionality?

The entities are at the bottom of the question to save space here. The objects are built up like this:

const whiteFlourEntity = new Ingredient();
whiteFlourEntity.name = "white flour";
whiteFlourEntity.percentage = 100;

const flourEntities: Ingredient[] = [whiteFlourEntity];

const waterEntity = new Ingredient();
waterEntity.name = "water";
waterEntity.percentage = 68;
waterEntity.waterPercentage = 100;

const yeastEntity = new Ingredient();
yeastEntity.name = "yeast";
yeastEntity.percentage = 1;

const saltEntity = new Ingredient();
saltEntity.name = "salt";
saltEntity.percentage = 2;

const formulaEntity = new Formula();
formulaEntity.name = "test formula";
formulaEntity.flours = flourEntities;
formulaEntity.water = waterEntity;
formulaEntity.yeast = yeastEntity;
formulaEntity.salt = saltEntity;

When I save all nested entities first and after that the main entity all relationships are set up right. But when I only save the main entity, the sub-entities are not saved.

createConnection({
    /** */
    "entities": [Ingredient, Formula]
}).then(async connection => {
    
    // WITH THIS LINES ACTIVE ALL RELATIONSHIPS ARE SAVED CORRECTLY
    // flourEntities.forEach((flour: Ingredient) => {
    //     connection.manager.save(flour);
    // });
    // await connection.manager.save(waterEntity );
    // await connection.manager.save(yeastEntity );
    // await connection.manager.save(saltEntity );

    await connection.manager.save(formulaEntity);

}).catch(error => console.log(error));
@Entity()
export default class Ingredient {

    @PrimaryGeneratedColumn()
    id?: number;

    @Column()
    name!: string;

    @Column()
    percentage!: number;

    @Column()
    waterPercentage!: number = 0;
}

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

    @Column()
    name!: string;

    @ManyToMany(() => Formula)
    @JoinTable()
    flours!: Ingredient[];

    @OneToOne(() => Ingredient)
    @JoinColumn()
    water!: Ingredient;

    @OneToOne(() => Ingredient)
    @JoinColumn()
    yeast!: Ingredient;

    @OneToOne(() => Ingredient)
    @JoinColumn()
    salt!: Ingredient;

    @ManyToMany(() => Ingredient)
    @JoinTable()
    extras?: Ingredient[];

    @ManyToMany(() => Formula)
    @JoinTable()
    subFormulas?: Formula[];
}
jrswgtr
  • 2,287
  • 8
  • 23
  • 49
  • "relation" does not mean what you think it means. – Dai Jan 24 '22 at 23:05
  • Those are _foreign-key relationships_, not _relations_. A ["relation"](https://en.wikipedia.org/wiki/Finitary_relation) is [the formal term in database-theory for an entire table](https://en.wikipedia.org/wiki/Relation_(database)) (and has nothing to do with foreign-keys or relationships themselves). – Dai Jan 24 '22 at 23:08
  • @Dai To be fair, TypeORM calls it relations themselves https://orkhan.gitbook.io/typeorm/docs/relations – jrswgtr Jan 26 '22 at 16:18

0 Answers0