Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

877
Visualizações
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;
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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 => postToCategory.product)
public productToStone!: PostToCategory[];
// stone.entity.ts
...
@OneToMany(() => ProductToStone, postToCategory => postToCategory.stone)
public postToCategories!: PostToCategory[];
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda