dosubot[bot] commented on issue #38976:
URL: https://github.com/apache/superset/issues/38976#issuecomment-4162834089

   <!-- Greeting -->
   Hi @rbu101! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the 
superset team.
   
   <!-- Answer -->
   This is a well-documented issue: when creating charts via `POST 
/api/v1/chart/`, the **`query_context` field is not auto-generated** by the 
backend — it's stored as `null` 
[[1]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/charts/api.py).
 Dashboard filter widgets depend on this field to execute their queries, and 
without it, they fail with "Empty query?" 
[[2]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/models/slice.py#L289-L298).
 The `query_context` is normally generated by the **frontend Explore UI** when 
you save a chart 
[[3]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset-frontend/src/explore/actions/saveModalActions.ts#L86-L183),
 which is why manually saving "fixes" it.
   
   **The fix** is to include `query_context` as a JSON string in your `POST 
/api/v1/chart/` call 
[[4]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/charts/schemas.py#L193-L197).
 For a `filter_select` chart, it should look something like:
   
   ```json
   POST /api/v1/chart/
   {
     "slice_name": "My Filter",
     "datasource_id": 1,
     "datasource_type": "table",
     "viz_type": "filter_select",
     "params": "{\"groupby\": [\"my_column\"], \"viz_type\": \"filter_select\", 
...}",
     "query_context": "{\"datasource\": {\"id\": 1, \"type\": \"table\"}, 
\"force\": false, \"queries\": [{\"columns\": [\"my_column\"], \"metrics\": [], 
\"filters\": [], \"extras\": {\"having\": \"\", \"where\": \"\"}, 
\"applied_time_extras\": {}, \"orderby\": [], \"row_limit\": 1000, 
\"post_processing\": []}], \"form_data\": {\"groupby\": [\"my_column\"], 
\"viz_type\": \"filter_select\"}, \"result_format\": \"json\", \"result_type\": 
\"full\"}"
   }
   ```
   
   The `queries[0].columns` field should contain the column(s) from your 
`groupby` config 
[[5]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset-frontend/src/filters/components/Select/buildQuery.ts#L57-L68).
 You can also update existing charts after the fact via `PUT 
/api/v1/chart/{chart_id}` with the `query_context` field.
   
   A [recent discussion 
thread](https://github.com/apache/superset/discussions/38718#discussioncomment-16207244)
 covers this exact problem in detail, and there was also a [DB migration PR 
(#33710)](https://github.com/apache/superset/pull/33710) to retroactively 
generate `query_context` for charts that are missing it 
[[6]](https://github.com/apache/superset/pull/33710). You can check if running 
that migration helps for your existing charts.
   
   
   <!-- Dosu Comment Footer -->
   *To reply, just mention 
[@dosu](https://go.dosu.dev/dosubot?utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=brand-link).*
   
   ---
   <sup>How did I do? 
[Good](https://app.dosu.dev/response-feedback/a73e97ba-8bbe-4844-a930-bfa99a68a05a?feedback_type=great_response&utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-great_response)
 | 
[Irrelevant](https://app.dosu.dev/response-feedback/a73e97ba-8bbe-4844-a930-bfa99a68a05a?feedback_type=irrelevant_answer&utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-irrelevant_answer)
 | 
[Incorrect](https://app.dosu.dev/response-feedback/a73e97ba-8bbe-4844-a930-bfa99a68a05a?feedback_type=incorrect_sources&utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-incorrect_sources)
 | 
[Verbose](https://app.dosu.dev/response-feedback/a73e97ba-8bbe-4844-a930-bfa99a68a05a?feedback_type=too_verbose&utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-too_verbose)
 | [Hallucination](https://app.dosu.dev/response-feedback/a73e97ba-8bbe
 
-4844-a930-bfa99a68a05a?feedback_type=hallucination&utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-hallucination)
 | [Report 
šŸ›](https://app.dosu.dev/response-feedback/a73e97ba-8bbe-4844-a930-bfa99a68a05a?feedback_type=bug_report&utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-bug_report)
 | 
[Other](https://app.dosu.dev/response-feedback/a73e97ba-8bbe-4844-a930-bfa99a68a05a?feedback_type=other&utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=feedback-other)</sup>
   
   [![Chat with 
Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/a28d3c7e-a9d3-459e-9fb6-3a6f9ff4f357/ask?utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=chat-badge)&nbsp;[![Open
 in 
Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=This%20is%20a%20well-documented%20issue%3A%20when%20creating%20charts%20via%20%60POST%20/api/v1/chart/%60%2C%20the%20%2A%2A%60query_context%60%20field%20is%20not%20auto-generated%2A%2A%20by%20the%20backend%20%E2%80%94%20it%27s%20stored%20as%20%60null%60%20%5B%5B1%5D%5D%28https%3A//github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/charts/api.py%29.%20Dashboard%20filter%20widgets%20depend%20on%20this%20field%20to%20execute%20their%20queries%2C%20and%20without%20it%2C%20they%20fail%20with%20%22Empty%20query%3F%22%20%5B%5B2%5D%5D%28https%3A//github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/models/slice.py%23L289-L
 
298%29.%20The%20%60query_context%60%20is%20normally%20generated%20by%20the%20%2A%2Afrontend%20Explore%20UI%2A%2A%20when%20you%20save%20a%20chart%20%5B%5B3%5D%5D%28https%3A//github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset-frontend/src/explore/actions/saveModalActions.ts%23L86-L183%29%2C%20which%20is%20why%20manually%20saving%20%22fixes%22%20it.%0A%0A%2A%2AThe%20fix%2A%2A%20is%20to%20include%20%60query_context%60%20as%20a%20JSON%20string%20in%20your%20%60POST%20/api/v1/chart/%60%20call%20%5B%5B4%5D%5D%28https%3A//github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/charts/schemas.py%23L193-L197%29.%20For%20a%20%60filter_select%60%20chart%2C%20it%20should%20look%20something%20like%3A%0A%0A%60%60%60json%0APOST%20/api/v1/chart/%0A%7B%0A%20%20%22slice_name%22%3A%20%22My%20Filter%22%2C%0A%20%20%22datasource_id%22%3A%201%2C%0A%20%20%22datasource_type%22%3A%20%22table%22%2C%0A%20%20%22viz_type%22%3A%20%22filter_select%22%2C%0A%20
 
%20%22params%22%3A%20%22%7B%5C%22groupby%5C%22%3A%20%5B%5C%22my_column%5C%22%5D%2C%20%5C%22viz_type%5C%22%3A%20%5C%22filter_select%5C%22%2C%20...%7D%22%2C%0A%20%20%22query_context%22%3A%20%22%7B%5C%22datasource%5C%22%3A%20%7B%5C%22id%5C%22%3A%201%2C%20%5C%22type%5C%22%3A%20%5C%22table%5C%22%7D%2C%20%5C%22force%5C%22%3A%20false%2C%20%5C%22queries%5C%22%3A%20%5B%7B%5C%22columns%5C%22%3A%20%5B%5C%22my_column%5C%22%5D%2C%20%5C%22metrics%5C%22%3A%20%5B%5D%2C%20%5C%22filters%5C%22%3A%20%5B%5D%2C%20%5C%22extras%5C%22%3A%20%7B%5C%22having%5C%22%3A%20%5C%22%5C%22%2C%20%5C%22where%5C%22%3A%20%5C%22%5C%22%7D%2C%20%5C%22applied_time_extras%5C%22%3A%20%7B%7D%2C%20%5C%22orderby%5C%22%3A%20%5B%5D%2C%20%5C%22row_limit%5C%22%3A%201000%2C%20%5C%22post_processing%5C%22%3A%20%5B%5D%7D%5D%2C%20%5C%22form_data%5C%22%3A%20%7B%5C%22groupby%5C%22%3A%20%5B%5C%22my_column%5C%22%5D%2C%20%5C%22viz_type%5C%22%3A%20%5C%22filter_select%5C%22%7D%2C%20%5C%22result_format%5C%22%3A%20%5C%22json%5C%22%2C%20%5C%22result
 
_type%5C%22%3A%20%5C%22full%5C%22%7D%22%0A%7D%0A%60%60%60%0A%0AThe%20%60queries%5B0%5D.columns%60%20field%20should%20contain%20the%20column%28s%29%20from%20your%20%60groupby%60%20config%20%5B%5B5%5D%5D%28https%3A//github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset-frontend/src/filters/components/Select/buildQuery.ts%23L57-L68%29.%20You%20can%20also%20update%20existing%20charts%20after%20the%20fact%20via%20%60PUT%20/api/v1/chart/%7Bchart_id%7D%60%20with%20the%20%60query_context%60%20field.%0A%0AA%20%5Brecent%20discussion%20thread%5D%28https%3A//github.com/apache/superset/discussions/38718%23discussioncomment-16207244%29%20covers%20this%20exact%20problem%20in%20detail%2C%20and%20there%20was%20also%20a%20%5BDB%20migration%20PR%20%28%2333710%29%5D%28https%3A//github.com/apache/superset/pull/33710%29%20to%20retroactively%20generate%20%60query_context%60%20for%20charts%20that%20are%20missing%20it%20%5B%5B6%5D%5D%28https%3A//github.com/apache/superset/pull/33
 
710%29.%20You%20can%20check%20if%20running%20that%20migration%20helps%20for%20your%20existing%20charts.)&nbsp;[![Join
 
Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot?utm_source=github&utm_medium=bot-comment&utm_campaign=github-assistant&utm_content=join-discord)&nbsp;[![Share
 on 
X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/apache/superset/issues/38976)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to