odata-v4-typeorm
odata-v4-typeorm copied to clipboard
Relation is duplicating ID unnecessarily
Sorry, don't really know how else to word that. This can be seen as one of the base example routes:
/api/posts - $filter by $expand
{{base_url}}/api/posts?$select=id,title,text,author&$expand=category,author&$filter=author/id eq 1
This returns
{
"id": 1,
"title": "Ultricies Sem Ltd",
"text": "quam. Curabitur vel lectus. Cum",
"category": {
"id": 1,
"name": "Eu Nulla Limited"
},
"author": {
"id": [
1,
1
],
"name": "Ursula Manning"
}
},
author.id is being duplicated and grouped.
I suspect that this is because it's returning the column twice
SQL that is produced:
SELECT "Post"."id" AS "Post_id"
, "Post"."title" AS "Post_title"
, "Post"."text" AS "Post_text"
, "category"."id" AS "category_id"
, "category"."name" AS "category_name"
, "author"."id" AS "author_id"
, "author"."name" AS "author_name"
, "Post"."author_id"
FROM "posts" "Post"
LEFT JOIN "post_category" "category" ON "category"."id"="Post"."category_id"
AND (1 = 1)
LEFT JOIN "authors" "author" ON "author"."id"="Post"."author_id"
AND (1 = 1)
WHERE "author"."id" = 1
Using MSSQL
This looks like it's a TypeORM bug related to MSSQL.
Test code (removes odata from the equation, just straight up TypeORM methods)
postsRepository.createQueryBuilder('Post')
.andWhere('author.id = :p0').setParameters({'p0': 1})
.select('Post.id, Post.title, Post.text, Post.author'.split(',').map(i => i.trim()))
.leftJoinAndSelect('Post.author', 'author', '1 = 1')
.getMany().then((data)=>{
req.status(200).json(data)
})
MSSQL:
[
{
"id": 1,
"title": "Ultricies Sem Ltd",
"text": "quam. Curabitur vel lectus. Cum",
"author": {
"id": [
1,
1
],
"name": "Ursula Manning"
}
},
{
"id": 6,
"title": "Nunc Interdum Feugiat LLC",
"text": "elit, pellentesque a, facilisis non, bibendum sed, est.",
"author": {
"id": [
1,
1
],
"name": "Ursula Manning"
}
}
]
SQLite:
[
{
"id": 1,
"title": "Ultricies Sem Ltd",
"text": "quam. Curabitur vel lectus. Cum",
"author": {
"id": 1,
"name": "Ursula Manning"
}
},
{
"id": 6,
"title": "Nunc Interdum Feugiat LLC",
"text": "elit, pellentesque a, facilisis non, bibendum sed, est.",
"author": {
"id": 1,
"name": "Ursula Manning"
}
}
]
SQLite seems to return the data correctly via TypeORM, so there's something funky going on with the MSSQL
Upstream issue: https://github.com/typeorm/typeorm/issues/7775
This project produces the 'Post.id, Post.title, Post.text, Post.author'
select statement, and I'm unsure if Post.author
is correct syntax to use for TypeORM (examples I've seen are things like 'Post.id, Post.title, Post.text, author'
). So dunno if I should keep this issue open or not.