mapper icon indicating copy to clipboard operation
mapper copied to clipboard

@Automap decorator conflicts with TypeORM

Open bfahm opened this issue 3 years ago • 1 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the issue

I have an entity (Shipment) with inner relation (inner model), let's say Organization and OrganizationId. I had the @AutoMap decorator for the Id field at first and everything was working great. But when I added the @AutoMap decorator to the inner model (Organization), TypeORM started complaining:

null value in column "organizationId" of relation "shipment" violates not-null constraint

And the resulted query of adding a new Shipment is as follows:

INSERT INTO "shipment"("id", "organizationId") VALUES (DEFAULT, DEFAULT)

Even though I have the organizationId field correctly set before saving the new entity:

export class ShipmentsRepository extends Repository<Shipment> {
  async createShipment(shipment: Shipment): Promise<Shipment> {
    // At this point, the shipment has an organization id
    shipment = this.create(shipment);
    // After creating the shipment, the shipment has NO organization id set
    return await this.save(entity); // Which is why this fails
  }
}

Here is my shipment model:

import { AutoMap } from '@automapper/classes';
import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
import { Organization } from '../../organization/persistance/organization.entity';

@Entity()
export class Shipment {
  @AutoMap()
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @AutoMap()
  @Column('uuid', { nullable: false })
  organizationId: string;
  @AutoMap() // Before adding this line, everything worked as expected, but after adding it, the problem occured
  @ManyToOne((type) => Organization, (organization) => organization.shipments, {
    eager: false,
  })
  organization: Organization;
}

Models/DTOs/VMs

No response

Mapping configuration

No response

Steps to reproduce

No response

Expected behavior

Adding an entity with relations through TypeORM works correctly.

Screenshots

No response

Minimum reproduction code

No response

Package

  • [ ] I don't know.
  • [ ] @automapper/core
  • [X] @automapper/classes
  • [ ] @automapper/nestjs
  • [ ] @automapper/pojos
  • [ ] @automapper/sequelize
  • [ ] @automapper/types
  • [ ] Other (see below)

Other package and its version

No response

AutoMapper version

7.2.1

Additional context

NestJs

bfahm avatar Feb 27 '22 08:02 bfahm

Hi @bfahm , sorry for the late response. Would you be able to test this with AutoMapper v8?

nartc avatar Apr 04 '22 02:04 nartc