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
This is a pretty annoying bug, in my case, if I remove
{eager: true}from child relationship’s inverseSide and instead specifyrelations: ['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_idASContactEntity_organization_id,ContactEntity_organization.idASContactEntity_organization_id,“typeorm”: “^0.2.29”,
@gabrielcipriano
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
I had the same issue, I was utilizing the
.findOnemethod and that’s when it generated a query with the same alias. I managed to fix this by changing from.findOneto.findand 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