diff --git a/public/app/core/utils/explore.ts b/public/app/core/utils/explore.ts index e59a72cf47c..5607898732f 100644 --- a/public/app/core/utils/explore.ts +++ b/public/app/core/utils/explore.ts @@ -268,13 +268,13 @@ export async function generateEmptyQuery( let defaultQuery: Partial | undefined; // datasource override is if we have switched datasources with no carry-over - we want to create a new query with a datasource we define + // it's also used if there's a root datasource and there were no previous queries if (dataSourceOverride) { datasourceRef = dataSourceOverride; } else if (queries.length > 0 && queries[queries.length - 1].datasource) { // otherwise use last queries' datasource datasourceRef = queries[queries.length - 1].datasource; } else { - // if neither exists, use the default datasource datasourceInstance = await getDataSourceSrv().get(); defaultQuery = datasourceInstance.getDefaultQuery?.(CoreApp.Explore); datasourceRef = datasourceInstance.getRef(); diff --git a/public/app/features/explore/state/query.ts b/public/app/features/explore/state/query.ts index 03713fa05c8..a7cf009c639 100644 --- a/public/app/features/explore/state/query.ts +++ b/public/app/features/explore/state/query.ts @@ -222,7 +222,18 @@ export const clearCacheAction = createAction('explore/clearCa export function addQueryRow(exploreId: ExploreId, index: number): ThunkResult { return async (dispatch, getState) => { const queries = getState().explore[exploreId]!.queries; - const query = await generateEmptyQuery(queries, index); + let datasourceOverride = undefined; + + // if this is the first query being added, check for a root datasource + // if it's not mixed, send it as an override. generateEmptyQuery doesn't have access to state + if (queries.length === 0) { + const rootDatasource = getState().explore[exploreId]!.datasourceInstance; + if (!config.featureToggles.exploreMixedDatasource || !rootDatasource?.meta.mixed) { + datasourceOverride = rootDatasource; + } + } + + const query = await generateEmptyQuery(queries, index, datasourceOverride?.getRef()); dispatch(addQueryRowAction({ exploreId, index, query })); };