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

359
Visualizações
TypeORM ManyToMany Juntion table create not working

I've got two tables services and offers. an Offer can have many services and a service can have many offers.

My service entity

@Entity('Services')
export class ServiceEntity extends BaseEntity {
 
  @PrimaryGeneratedColumn()
  serviceKey: number;
  
  ....
  
  @ManyToMany(() => OffersEntity, (offer) => offer.services)
  @JoinTable({
    name: 'offersXservices',
    inverseJoinColumn: {
        name: 'offerKey',
        referencedColumnName: 'offerKey'
    },
    joinColumn: {
        name: 'serviceKey',
        referencedColumnName: 'serviceKey',
    },
   })
   offers?: OffersEntity[];

}

And My Offers entity is defined like this:

@Entity('Offers')
export class OffersEntity extends BaseEntity {
  @PrimaryGeneratedColumn()
  offerKey: number;
  
  ....
  
  @ManyToMany(() => ServiceEntity, (service) => service.offers)
  @JoinTable({
    name: 'offersXservices',
    inverseJoinColumn: {
        name: 'serviceKey',
        referencedColumnName: 'serviceKey'
    },
    joinColumn: {
        name: 'offerKey',
        referencedColumnName: 'offerKey',
    },
   })
   services?: ServiceEntity[];
}

I'm getting an error when I tried to create an offer record with some services : query failed: INSERT INTO "offersXservices"("offerKey", "serviceKey") VALUES (DEFAULT, DEFAULT), (DEFAULT, DEFAULT) error: error: null value in column "offerKey" violates not-null constraint.

I don't understand why this is happening? in the above generated query the DEFAULT should be replaced with offerKey and serviceKey respectively. can anyone help me on that?

Here is the code which I use to insert into offer record.

const _offerEntity = await OffersEntity.create({
            vendorKey: input.vendorKey,
            name: input.name,
            description: input.description,
            startAt: input.startAt,
            expiry: input.expiry,
            services: await ServiceEntity.find({
                where: {
                    serviceKey: In(input.services)
                }
            })
        });

 await queryRunner.manager.save(_offerEntity);

Any help would be appreciated :)

PS: Database is postgresql

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I didn't find a proper solution to this question. I ended up using raw sql queries with typeorm.

for (const _service of input.services) {
   await queryRunner.manager.query(`INSERT INTO "offersXservices"("offerKey", "serviceKey") VALUES (${_offerEntity.offerKey}, ${_service})`);
}

Hope it helps someone.

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