Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate column name with TypeORM in Nestjs

We have 2 entities: EstateIntegrationEntity and EstateEntity

When we try to use .findOne on the estateIntegrationRepository we get the following error:

[Nest] 5537   - 10/01/2020, 8:37:55 AM
[ExceptionsHandler] ER_DUP_FIELDNAME: Duplicate column name 'EstateIntegrationEntity_estate_id' +1590ms
QueryFailedError: ER_DUP_FIELDNAME: Duplicate column name 'EstateIntegrationEntity_estate_id'
    at ...

We created a OneToOne relation from the EstateIntegrationEntity to the EstateEntity.

import { EstateEntity } from "src/estates/estate.entity";
import { Column, Entity, JoinColumn, OneToOne, PrimaryGeneratedColumn, RelationId } from "typeorm";

@Entity({ name: 'estate_integrations' })
export class EstateIntegrationEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @RelationId((estateIntegrationEntity: EstateIntegrationEntity) => estateIntegrationEntity.estate)
  estate_id: number;

  @OneToOne(() => EstateEntity, { eager: true })
  @JoinColumn({ name: 'estate_id' })
  estate: EstateEntity;

  ...
}

And a relation from the EstateEntity to the EstateIntegrationEntity :

import { EstateIntegrationEntity } from 'src/integrations/estate.integration.entity';
import { Column, Entity, JoinColumn, ManyToOne, OneToOne, PrimaryGeneratedColumn } from 'typeorm';

@Entity('estates')
export class EstateEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column('varchar' || null)
  uuid: string;

  @OneToOne(
    () => EstateIntegrationEntity,
    estate_integration => estate_integration.estate,
  )
  estate_integration: EstateIntegrationEntity;
}

This error only occurs with .findOne() and not with .find():

async findEstateById(id: string): Promise<EstateIntegrationEntity> {
  return await this.estateIntegrationRepository.findOne({
    where: {
      external_id: id
    }
  });
}
like image 411
Sven Peeters Avatar asked Sep 13 '25 01:09

Sven Peeters


1 Answers

You can change the name of the fields. I just had a similar error and fixed it changing the name with @Column({ name: 'name_the_fields_with_different_name' }). Because the problem that if in the tables it has the same name when it does the find it has to fields with the same name.

like image 83
AyadRocketfy Avatar answered Sep 14 '25 16:09

AyadRocketfy