a query in sql brings me a result, which is correct, but I use the same query in typeorm with nest and it only brings me one of the three columns that I select, is it a bug? I have country.name, city.name and profile.name and it only returns the country.name but nevertheless the query in the same database brings me the result with the 3 columns
#TypeORM
1 messages · Page 1 of 1 (latest)
I can't call three columns in different tables with the same name "name" in typeorm?
What does row look like here?
this is the query
Okay, but what does the typeorm return look like?
typeORM only return country.name
async findUser(id: number) { try { this.logger.info('searching user by req.user.id', { repository: UserRepository.name, }); const row = await this.dataSource.query( 'SELECT profile.name, city.name, country.name FROM user INNER JOIN profile ON user.id = profile.userId INNER JOIN address ON profile.addressId = address.id INNER JOIN city ON address.cityId = city.id INNER JOIN country ON city.countryId = country.id where user.id = ?', [id], );
Okay. That would most likely be because it doesn't know how to serialize name from each table. SQL is able to jsut say "Hey, this column is "name"" and you can refer back to the query. JSON doesn't have that ability, so TypeORM does
const obj = {};
obj.name = 'zazaza'
obj.name = 'Seattle'
obj.name = 'USA'
And the final output because of that is obj.name = 'USA'
Just a difference of lanagues.
Side note: you really should be aliasing those columns. For readability in the query result, and in the TypeO result
Thank you very much for the explanation, I did not know that, so with a
SELECT column_name AS alias_name FROM table_name;
should i fix that mapping?
I believe that will do it
yep 😄 It is working!
to format the response from the controller, the best solution is the interceptors?
Depends. Do you just need it once? In the service or controller is fine. Do you want to everywhere and consistent with the rest of the application? Do it in an interceptor.