fastcrud icon indicating copy to clipboard operation
fastcrud copied to clipboard

Duplicate Values when use relationship type one-to-many in a JoinConfig with get_multi_joined

Open gr1nch3 opened this issue 1 year ago • 1 comments

Describe the bug or question I'm trying to get data from tables where a user (table 1) can be a part of a company (table 2) and have multiple posts (table 3) on the company's forum. using get_multi_joined and joins_config, I want to get each user's company info and a list (won't be more than 5 items) of their posts on the forum but the posts are returning duplicate values.

To Reproduce

# Your code here
crud_user.get_multi_joined(
    "db": db,
    "is_deleted": False,
    "schema_to_select": UserModelCompPostsRead,
    "nest_joins": True,
    "joins_config": [
        JoinConfig(
            model=CompanyInfo,
            join_on=User.company_id == CompanyInfo.id, 
            join_prefix="company",
            schema_to_select=CompanyInfoRead,
        )
       JoinConfig(
            model=ForumPosts,
            join_on=User.id == ForumPosts.user_id,
            join_prefix="forum_posts",
            schema_to_select=ForumPosts,
            relationship_type="one-to-many",
        )
    ]
)

Description I'm receiving duplicates in the list of posts when the expected results should be an item in the list once it meets the join_on requirement.

expectation:

"data": [
        {
            "name": "John Kin",
            "id": 2,
            "company_id": 1,
            "company": {
                "name": "Company",
                "created_at": "2024-07-11T18:58:41.460483Z"
            },
            "forum_posts": [
                {
                    "title": "Money Talks",
					"user_id": 2,
                    "id": 41,
                },
                {
                    "title": "Green Goblin vs Spiderman",
                    "user_id": 2,
                    "id": 42,
                },
            ]
        },
		...
		# other users
	]

actual output:

"data": [
        {
            "name": "John Kin",
            "id": 2,
            "company_id": 1,
            "company": {
                "name": "Company",
                "created_at": "2024-07-11T18:58:41.460483Z"
            },
            "forum_posts": [
                {
                    "title": "Money Talks",
					"user_id": 2,
                    "id": 41,
                },
                {
                    "title": "Green Goblin vs Spiderman",
                    "user_id": 2,
                    "id": 42,
                },
				# values above are returned below too (duplicates)
				{
                    "title": "Money Talks",
					"user_id": 2,
                    "id": 41,
                },
                {
                    "title": "Green Goblin vs Spiderman",
                    "user_id": 2,
                    "id": 42,
                },
            ]
        },
		...
		# other users
	]

Additional context fastcrud = "^0.13.1" SQLAlchemy = "^2.0.25" fastapi = "^0.109.1"

gr1nch3 avatar Jul 29 '24 08:07 gr1nch3

Related to #115 @mithun2003 are you able to fix it?

igorbenav avatar Jul 30 '24 05:07 igorbenav

Can this be tagged as a bug. Is this on priority fix?

swathiluns avatar Oct 29 '24 17:10 swathiluns

Does it fix yet because i also have that issue.

**{
  "data": [
    {
      .....
      "roles": [
        {
          "id": 1,
          "name": "Role1",
          "created_at": "2024-12-07T02:52:01.343688+00:00",
          "updated_at": null
        },
        {
          "id": 2,
          "name": "Admin",
          "created_at": "2024-12-07T05:12:04.755316+00:00",
          "updated_at": null
        },
        {
          "id": 2,
          "name": "Admin",
          "created_at": "2024-12-07T05:12:04.755316+00:00",
          "updated_at": null
        },
        {
          "id": 1,
          "name": "Role1",
          "created_at": "2024-12-07T02:52:01.343688+00:00",
          "updated_at": null
        },
        {
          "id": 2,
          "name": "Admin",
          "created_at": "2024-12-07T05:12:04.755316+00:00",
          "updated_at": null
        },
        {
          "id": 2,
          "name": "Admin",
          "created_at": "2024-12-07T05:12:04.755316+00:00",
          "updated_at": null
        }
      ]
    }
  ],
  "total_count": 3,
  "has_more": false,
  "page": 1,
  "items_per_page": 10
}**

sombathoudom avatar Dec 09 '24 09:12 sombathoudom

I'll upload a fix later this week.

igorbenav avatar Dec 09 '24 18:12 igorbenav

Thanks you. but 1 more issue. { "data": [ { ..... "roles": [ { "id": 1, "name": "Role1", "created_at": "2024-12-07T02:52:01.343688+00:00", "updated_at": null }, { "id": 2, "name": "Admin", "created_at": "2024-12-07T05:12:04.755316+00:00", "updated_at": null }, { "id": 2, "name": "Admin", "created_at": "2024-12-07T05:12:04.755316+00:00", "updated_at": null }, { "id": 1, "name": "Role1", "created_at": "2024-12-07T02:52:01.343688+00:00", "updated_at": null }, { "id": 2, "name": "Admin", "created_at": "2024-12-07T05:12:04.755316+00:00", "updated_at": null }, { "id": 2, "name": "Admin", "created_at": "2024-12-07T05:12:04.755316+00:00", "updated_at": null } ] } ], "total_count": 3, "has_more": false, "page": 1, "items_per_page": 10 } this record have only 1 user and that users has 3 role but "total_count": 3 that is wrong that should be "total_count": 1

sombathoudom avatar Dec 10 '24 01:12 sombathoudom