AmirChegini
AmirChegini

Reputation: 33

TypeORM: Custom Many To Many Relationship

I'm working with Nest.js, TypeORM, and PostgreSQL, I have two entities(product and stone) with a many to many relation, based on my own business project, I have to add one extra column to that many to many table(product_stone), but I have some issue with my solution, at first I try to create a product with a set of stones:

"stones": [
    {"id": 1,"count":1},
    {"id": 2,"count": 3}
]

and after that, I try to add the count to the product_stone table by updating it, the result will be like this: product_stone_table till here everything is Okay, but every time that I restart the server all of the data in that extra column will be set to its default value(null): product_stone_table

And also I tried to do not set the count to {nullable:true} in product_stone table and add count during the creation of a product, but when I want to restart the server I receive an error kile this:

QueryFailedError: column "count" of relation "product_stone" contains null values

Is there anybody to guide me?

product.entity.ts

@Entity()
export class Product extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @ManyToMany(() => Stone)
  @JoinTable({
    name: 'product_stone',
    joinColumn: {
      name: 'productId',
      referencedColumnName: 'id',
    },
    inverseJoinColumn: {
      name: 'stoneId',
      referencedColumnName: 'id',
    },
  })
  stones: Stone[];
}

stone.entity.ts

@Entity()
export class Stone extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: string;
}

product_stone.entity.ts

@Entity('product_stone')
export class ProductStone extends BaseEntity {
  @Column({ nullable: true })
  count: number;

  @Column()
  @IsNotEmpty()
  @PrimaryColumn()
  productId: number;

  @Column()
  @IsNotEmpty()
  @PrimaryColumn()
  stoneId: number;
}

Upvotes: 3

Views: 3610

Answers (1)

metodribic
metodribic

Reputation: 1729

I don't think you can define custom attributes on many-to-many table like that.

From documentation:

In case you need to have additional properties in your many-to-many relationship, you have to create a new entity yourself

In your case that would mean you would have to so something like that:

// product_stone.entity.ts
@Entity()
export class ProductToStone {
    @PrimaryGeneratedColumn()
    public id: number;

    @Column()
    public productId: number;

    @Column()
    public stoneId: number;

    @Column()
    public count: number;

    @ManyToOne(() => Product, product => product.productToStone)
    public product: Product;

    @ManyToOne(() => Stone, stone => stone.productToStone)
    public stone: Stone;
}
// product.entity.ts
...
@OneToMany(() => ProductToStone, productToStone => productToStone.product)
public productToStones!: ProductToStone[];
// stone.entity.ts
...
@OneToMany(() => ProductToStone, productToStone => productToStone.stone)
public productToStones!: ProductToStone[];

Upvotes: 3

Related Questions