typeorm: QueryFailedError:` Duplicate column name ' error caused by a bad alias naming

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver: all

TypeORM version: [x] latest [] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem

Let’s take a simple example with 2 tables with a OneToOne cardinality: article(#id, category_id., ..) and article_category(#id, ...). So one article has only one category.

a findOne will generate alias naming like that :

`Article`.`category_id` AS `Article_category_id`,
`Article_category`.`id` AS `Article_category_id`,

And so an error:

QueryFailedError:` Duplicate column name 'Article_category_id' 

_Originally posted by @mohamed-badaoui in https://github.com/typeorm/typeorm/issues/1969#issuecomment-687055194_

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 15
  • Comments: 21

Most upvoted comments

This is a pretty annoying bug, in my case, if I remove {eager: true} from child relationship’s inverseSide and instead specify relations: ['child'] on the findOne method, the problem goes away. It seems to use different name strategy. I have also noticed that this behavior is different on different database drivers. For instance, it currently only happens when I’m using a Mysql database, which sucks because my integration tests all pass since I’m using Sqlite for them.

I am seeing the same ContactEntity.organization_id AS ContactEntity_organization_id, ContactEntity_organization.id AS ContactEntity_organization_id,

@ManyToOne(() => OrganizationEntity, (org) => org.id, { eager: true })
@JoinColumn({ name: 'organization_id' })
organization!: OrganizationEntity;

“typeorm”: “^0.2.29”,

@Entity('organization')
export class OrganizationEntity extends BaseEntity {
    @PrimaryGeneratedColumn()
    id!: number;

@gabrielcipriano

  @Column({ name: 'box_id', unsigned: true, select: false })
  boxId: number;

I tried it this way, but it broke my migration generation. TypeORM wasn’t able to properly create the foreign keys.

Going to try it out @abelncm 's way

Same issue here. Is there any workaround without changing table or column names?

I had the same issue, I was utilizing the .findOne method and that’s when it generated a query with the same alias. I managed to fix this by changing from .findOne to .find and then getting the first element from the array.

Not sure how this would affect in terms of performance, but it’s a quick workaround until this is fixed.

Same issue