As long as an administrator is assigned to the workspace, an error will be reported when calling the API. #973

Closed
opened 2026-02-21 17:29:18 -05:00 by yindo · 5 comments
Owner

Originally created by @IAMZn1018 on GitHub (Jan 25, 2024).

Originally assigned to: @crazywoola on GitHub.

Self Checks

Dify version

0.5.0 and 0.4.9

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

When there is an administrator in my workspace and I call the knowledge API

import requests

headers = {
    "Authorization": "Bearer dataset-vmFnLm3LkmAdwuLzXerAJUbq",
    "Content-Type": "application/json"
}

requests.get("http://10.254.22.222/v1/datasets?page=1&limit=20", headers=headers).json()

I get the following error:
微信图片_20240125210555

After reading the source code, I found that there is a problem with the following code
image
one_or_none() means that an error will be reported when there are multiple query results.

I checked the postgres database and found that the tenant_id of owner and admin are the same.
image
After looking at the tables account, tenant_account_joins, api_tokens, and tenants, I thought the best solution is to change the code to
image
It is completely unknown from the token which account is logging in, so there is no need to judge the account below in the code.You only need to determine whether the token was created by the owner.

I'm not sure if the code in the cloud version is the same as on github, so my suggestion is just the code on github

✔️ Expected Behavior

get the success response

Actual Behavior

No response

Originally created by @IAMZn1018 on GitHub (Jan 25, 2024). Originally assigned to: @crazywoola on GitHub. ### Self Checks - [X] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [X] I confirm that I am using English to file this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). ### Dify version 0.5.0 and 0.4.9 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce When there is an administrator in my workspace and I call the **knowledge** API ``` import requests headers = { "Authorization": "Bearer dataset-vmFnLm3LkmAdwuLzXerAJUbq", "Content-Type": "application/json" } requests.get("http://10.254.22.222/v1/datasets?page=1&limit=20", headers=headers).json() ``` I get the following error: <img width="635" alt="微信图片_20240125210555" src="https://github.com/langgenius/dify/assets/24623176/82b0661e-525c-414f-a3ae-4b4bfe51309e"> After reading the source code, I found that there is a problem with the following code ![image](https://github.com/langgenius/dify/assets/24623176/0b6712e3-cf31-4427-8e19-1e43bb23862e) **one_or_none()** means that an error will be reported when there are multiple query results. I checked the postgres database and found that the **tenant_id** of owner and admin are the same. ![image](https://github.com/langgenius/dify/assets/24623176/14539367-97fc-4b2b-9080-21183c91a47d) After looking at the tables account, tenant_account_joins, api_tokens, and tenants, I thought the best solution is to change the code to ![image](https://github.com/langgenius/dify/assets/24623176/93581bc0-05ce-4512-9c63-5296ac8b2838) It is completely unknown from the token which account is logging in, so there is no need to judge the account below in the code.You only need to determine whether the token was created by the owner. I'm not sure if the code in the cloud version is the same as on github, so my suggestion is just the code on github ### ✔️ Expected Behavior get the success response ### ❌ Actual Behavior _No response_
yindo added the 🐞 bug label 2026-02-21 17:29:18 -05:00
yindo closed this issue 2026-02-21 17:29:18 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jan 25, 2024):

🤖

It's great to see that you've identified a potential issue and even proposed a solution for it! Your thorough investigation and proposed code change are valuable contributions to the project. I encourage you to go ahead and create a pull request with your proposed code changes. Your contribution will help improve the project for everyone. Keep up the good work!


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@dosubot[bot] commented on GitHub (Jan 25, 2024): _🤖_ <!-- Answer --> It's great to see that you've identified a potential issue and even proposed a solution for it! Your thorough investigation and proposed code change are valuable contributions to the project. I encourage you to go ahead and create a pull request with your proposed code changes. Your contribution will help improve the project for everyone. Keep up the good work! --- This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot.
Author
Owner

@bowenliang123 commented on GitHub (Jan 25, 2024):

I've different suggestions for this problem.
Although removing ’admin‘ helps to avoid the error, it does prevent admin users to use the API.
How about changing .one_or_none to .first to bypass the multiple result rows situation?

@takatost

@bowenliang123 commented on GitHub (Jan 25, 2024): I've different suggestions for this problem. Although removing ’admin‘ helps to avoid the error, it does prevent admin users to use the API. How about changing `.one_or_none` to `.first` to bypass the multiple result rows situation? @takatost
Author
Owner

@crazywoola commented on GitHub (Jan 25, 2024):

I'm not sure if the code in the cloud version is the same as on github, so my suggestion is just the code on github

We do use the same code. :) @IAMZn1018

@crazywoola commented on GitHub (Jan 25, 2024): > I'm not sure if the code in the cloud version is the same as on github, so my suggestion is just the code on github We do use the same code. :) @IAMZn1018
Author
Owner

@IAMZn1018 commented on GitHub (Jan 25, 2024):

Because the tenant_id of all tenant_account_joins is the same, even if you change one_or_none() to first(), the effect is the same, and there is no judgment whether there is an admin user

@IAMZn1018 commented on GitHub (Jan 25, 2024): Because the tenant_id of all tenant_account_joins is the same, even if you change one_or_none() to first(), the effect is the same, and there is no judgment whether there is an admin user
Author
Owner

@IAMZn1018 commented on GitHub (Jan 25, 2024):

Because the tenant_id of all tenant_account_joins is the same, even if you change one_or_none() to first(), the effect is the same, and there is no judgment whether there is an admin user

sorry, I made a mistake

@IAMZn1018 commented on GitHub (Jan 25, 2024): > Because the tenant_id of all tenant_account_joins is the same, even if you change one_or_none() to first(), the effect is the same, and there is no judgment whether there is an admin user sorry, I made a mistake
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#973