Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relation is duplicating ID unnecessarily #27

Open
blitzmann opened this issue Jun 18, 2021 · 2 comments
Open

Relation is duplicating ID unnecessarily #27

blitzmann opened this issue Jun 18, 2021 · 2 comments

Comments

@blitzmann
Copy link
Contributor

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
image

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

@blitzmann
Copy link
Contributor Author

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

@blitzmann
Copy link
Contributor Author

Upstream issue: typeorm/typeorm#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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant