OperationalError: sub-select returns 2 columns - expected 1 in Django QuerySetHow to combine 2 or more querysets in a Django view?How do I do a not equal in Django queryset filtering?django - convert a list back to a querysetGetting the SQL from a Django QuerySetDjango queryset count with extra selectDjango queryset .count() .filter() not working on custom model managerDjango, How to make multiple annotate in a single querysetDjango filter a reverse relationship of each item in a querysetAnnotate not returning the expected resultsDjango queryset for multilevel Foreignkey reference

What is /etc/mtab in Linux?

Magical attacks and overcoming damage resistance

Zonal Statistics is returning null values in ArcGIS

What is the unit of time_lock_delta in LND?

Does a large simulator bay have standard public address announcements?

Is Electric Central Heating worth it if using Solar Panels?

How can I get rid of an unhelpful parallel branch when unpivoting a single row?

"My boss was furious with me and I have been fired" vs. "My boss was furious with me and I was fired"

How did Captain America manage to do this?

"Whatever a Russian does, they end up making the Kalashnikov gun"? Are there any similar proverbs in English?

I preordered a game on my Xbox while on the home screen of my friend's account. Which of us owns the game?

Why did C use the -> operator instead of reusing the . operator?

Combinatorics problem, right solution?

Partitioning values in a sequence

Can a level 2 Warlock take one level in rogue, then continue advancing as a warlock?

Where was the County of Thurn und Taxis located?

Why do games have consumables?

My admission is revoked after accepting the admission offer

Don’t seats that recline flat defeat the purpose of having seatbelts?

How much cash can I safely carry into the USA and avoid civil forfeiture?

Can a barbarian keep raging if she shoves an enemy on her turn?

Find the identical rows in a matrix

Should the Product Owner dictate what info the UI needs to display?

What does a straight horizontal line above a few notes, after a changed tempo mean?



OperationalError: sub-select returns 2 columns - expected 1 in Django QuerySet


How to combine 2 or more querysets in a Django view?How do I do a not equal in Django queryset filtering?django - convert a list back to a querysetGetting the SQL from a Django QuerySetDjango queryset count with extra selectDjango queryset .count() .filter() not working on custom model managerDjango, How to make multiple annotate in a single querysetDjango filter a reverse relationship of each item in a querysetAnnotate not returning the expected resultsDjango queryset for multilevel Foreignkey reference






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















There are two models a Price model and Service model. I'm trying to find the Services that have the most prices. All of this is initially filtered by a user input query (called the entry_query). The first line (1) get the price objects that the user queries (this works). Then, line 2 (2) returns a QS with the service_code and counts of prices a service has. Then, it brings on line (3) where it gives an error (see below).



(1) Price_objs = Price_filter.filter(entry_query) 

(2) objs_filter=Price_objs.values_list('service__code').annotate(service_count=Count('service__code')).order_by('-service_count')

(3) serv_obj = Service.objects.filter(price__in = objs_filter).distinct()


Here is what line (2) outputs:



<QuerySet [('36430', 62), ('86003', 28), ('87149', 28), ('83516', 23), ('86317', 20), ('94640', 19), ('73502', 18), ('86658', 14), ('73721', 13), ('87070', 13), ('76942', 12), ('87081', 12), ('73560', 11), ('87798', 11), ('36415', 10), ('74177', 10), ('99211', 10), ('73100', 9), ('73221', 9), ('74176', 9), '...(remaining elements truncated)...']>


Here is the error that I get with line (3):
django.db.utils.OperationalError: sub-select returns 2 columns - expected 1










share|improve this question



















  • 1





    I don't understand what you are doing here. Why are you annotating the service_count on the Price_objs query?

    – Daniel Roseman
    Mar 22 at 16:51












  • The price table has prices for services. I want to count how many prices there are for one service code - like service code '36430' has 62 prices. I want to dsiplay the Services with the most prices, so I want to order Service results by the number of prices that are available.

    – J R
    Mar 22 at 17:05







  • 2





    The other problem you have is, ordering the Price sub-select won't order the Service queryset. You should probably start from Service and annotate price__... as related lookups. Service.objects.filter(price__...entry_query...).annotate(price_count=Count('price_set').order_by('-price_count') Or something similar to this.

    – Geekfish
    Mar 22 at 17:20












  • This is the answer that I was looking for thanks. It worked.

    – J R
    Mar 23 at 2:53

















0















There are two models a Price model and Service model. I'm trying to find the Services that have the most prices. All of this is initially filtered by a user input query (called the entry_query). The first line (1) get the price objects that the user queries (this works). Then, line 2 (2) returns a QS with the service_code and counts of prices a service has. Then, it brings on line (3) where it gives an error (see below).



(1) Price_objs = Price_filter.filter(entry_query) 

(2) objs_filter=Price_objs.values_list('service__code').annotate(service_count=Count('service__code')).order_by('-service_count')

(3) serv_obj = Service.objects.filter(price__in = objs_filter).distinct()


Here is what line (2) outputs:



<QuerySet [('36430', 62), ('86003', 28), ('87149', 28), ('83516', 23), ('86317', 20), ('94640', 19), ('73502', 18), ('86658', 14), ('73721', 13), ('87070', 13), ('76942', 12), ('87081', 12), ('73560', 11), ('87798', 11), ('36415', 10), ('74177', 10), ('99211', 10), ('73100', 9), ('73221', 9), ('74176', 9), '...(remaining elements truncated)...']>


Here is the error that I get with line (3):
django.db.utils.OperationalError: sub-select returns 2 columns - expected 1










share|improve this question



















  • 1





    I don't understand what you are doing here. Why are you annotating the service_count on the Price_objs query?

    – Daniel Roseman
    Mar 22 at 16:51












  • The price table has prices for services. I want to count how many prices there are for one service code - like service code '36430' has 62 prices. I want to dsiplay the Services with the most prices, so I want to order Service results by the number of prices that are available.

    – J R
    Mar 22 at 17:05







  • 2





    The other problem you have is, ordering the Price sub-select won't order the Service queryset. You should probably start from Service and annotate price__... as related lookups. Service.objects.filter(price__...entry_query...).annotate(price_count=Count('price_set').order_by('-price_count') Or something similar to this.

    – Geekfish
    Mar 22 at 17:20












  • This is the answer that I was looking for thanks. It worked.

    – J R
    Mar 23 at 2:53













0












0








0








There are two models a Price model and Service model. I'm trying to find the Services that have the most prices. All of this is initially filtered by a user input query (called the entry_query). The first line (1) get the price objects that the user queries (this works). Then, line 2 (2) returns a QS with the service_code and counts of prices a service has. Then, it brings on line (3) where it gives an error (see below).



(1) Price_objs = Price_filter.filter(entry_query) 

(2) objs_filter=Price_objs.values_list('service__code').annotate(service_count=Count('service__code')).order_by('-service_count')

(3) serv_obj = Service.objects.filter(price__in = objs_filter).distinct()


Here is what line (2) outputs:



<QuerySet [('36430', 62), ('86003', 28), ('87149', 28), ('83516', 23), ('86317', 20), ('94640', 19), ('73502', 18), ('86658', 14), ('73721', 13), ('87070', 13), ('76942', 12), ('87081', 12), ('73560', 11), ('87798', 11), ('36415', 10), ('74177', 10), ('99211', 10), ('73100', 9), ('73221', 9), ('74176', 9), '...(remaining elements truncated)...']>


Here is the error that I get with line (3):
django.db.utils.OperationalError: sub-select returns 2 columns - expected 1










share|improve this question
















There are two models a Price model and Service model. I'm trying to find the Services that have the most prices. All of this is initially filtered by a user input query (called the entry_query). The first line (1) get the price objects that the user queries (this works). Then, line 2 (2) returns a QS with the service_code and counts of prices a service has. Then, it brings on line (3) where it gives an error (see below).



(1) Price_objs = Price_filter.filter(entry_query) 

(2) objs_filter=Price_objs.values_list('service__code').annotate(service_count=Count('service__code')).order_by('-service_count')

(3) serv_obj = Service.objects.filter(price__in = objs_filter).distinct()


Here is what line (2) outputs:



<QuerySet [('36430', 62), ('86003', 28), ('87149', 28), ('83516', 23), ('86317', 20), ('94640', 19), ('73502', 18), ('86658', 14), ('73721', 13), ('87070', 13), ('76942', 12), ('87081', 12), ('73560', 11), ('87798', 11), ('36415', 10), ('74177', 10), ('99211', 10), ('73100', 9), ('73221', 9), ('74176', 9), '...(remaining elements truncated)...']>


Here is the error that I get with line (3):
django.db.utils.OperationalError: sub-select returns 2 columns - expected 1







django django-queryset






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 17:07







J R

















asked Mar 22 at 16:48









J RJ R

6318




6318







  • 1





    I don't understand what you are doing here. Why are you annotating the service_count on the Price_objs query?

    – Daniel Roseman
    Mar 22 at 16:51












  • The price table has prices for services. I want to count how many prices there are for one service code - like service code '36430' has 62 prices. I want to dsiplay the Services with the most prices, so I want to order Service results by the number of prices that are available.

    – J R
    Mar 22 at 17:05







  • 2





    The other problem you have is, ordering the Price sub-select won't order the Service queryset. You should probably start from Service and annotate price__... as related lookups. Service.objects.filter(price__...entry_query...).annotate(price_count=Count('price_set').order_by('-price_count') Or something similar to this.

    – Geekfish
    Mar 22 at 17:20












  • This is the answer that I was looking for thanks. It worked.

    – J R
    Mar 23 at 2:53












  • 1





    I don't understand what you are doing here. Why are you annotating the service_count on the Price_objs query?

    – Daniel Roseman
    Mar 22 at 16:51












  • The price table has prices for services. I want to count how many prices there are for one service code - like service code '36430' has 62 prices. I want to dsiplay the Services with the most prices, so I want to order Service results by the number of prices that are available.

    – J R
    Mar 22 at 17:05







  • 2





    The other problem you have is, ordering the Price sub-select won't order the Service queryset. You should probably start from Service and annotate price__... as related lookups. Service.objects.filter(price__...entry_query...).annotate(price_count=Count('price_set').order_by('-price_count') Or something similar to this.

    – Geekfish
    Mar 22 at 17:20












  • This is the answer that I was looking for thanks. It worked.

    – J R
    Mar 23 at 2:53







1




1





I don't understand what you are doing here. Why are you annotating the service_count on the Price_objs query?

– Daniel Roseman
Mar 22 at 16:51






I don't understand what you are doing here. Why are you annotating the service_count on the Price_objs query?

– Daniel Roseman
Mar 22 at 16:51














The price table has prices for services. I want to count how many prices there are for one service code - like service code '36430' has 62 prices. I want to dsiplay the Services with the most prices, so I want to order Service results by the number of prices that are available.

– J R
Mar 22 at 17:05






The price table has prices for services. I want to count how many prices there are for one service code - like service code '36430' has 62 prices. I want to dsiplay the Services with the most prices, so I want to order Service results by the number of prices that are available.

– J R
Mar 22 at 17:05





2




2





The other problem you have is, ordering the Price sub-select won't order the Service queryset. You should probably start from Service and annotate price__... as related lookups. Service.objects.filter(price__...entry_query...).annotate(price_count=Count('price_set').order_by('-price_count') Or something similar to this.

– Geekfish
Mar 22 at 17:20






The other problem you have is, ordering the Price sub-select won't order the Service queryset. You should probably start from Service and annotate price__... as related lookups. Service.objects.filter(price__...entry_query...).annotate(price_count=Count('price_set').order_by('-price_count') Or something similar to this.

– Geekfish
Mar 22 at 17:20














This is the answer that I was looking for thanks. It worked.

– J R
Mar 23 at 2:53





This is the answer that I was looking for thanks. It worked.

– J R
Mar 23 at 2:53












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304323%2foperationalerror-sub-select-returns-2-columns-expected-1-in-django-queryset%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304323%2foperationalerror-sub-select-returns-2-columns-expected-1-in-django-queryset%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript