postgresql - more than one row returned by a subquery used as an expression -


all sqlalchemy models query fine, there 1 gives me:

more 1 row returned subquery used expression 

this (shortened) sql generated sqlalchemy:

select poi.key ,poi.pok ,poi.noc ,coalesce((     select case          when (sum(item.noc) null)             null         else :param_1         end anon_1     item,poi     poi.key = item.poi_key     group item.key     ), poi.noc) coalesce_1 ,coalesce((     select sum(soi.noc) sum_1     soi     soi.poi_key = poi.key         , soi.is_shipped = 0     ), :param_2) coalesce_2 ,coalesce((     select case          when (sum(item.noc) null)             null         else :param_1         end anon_1     item,poi     poi.key = item.poi_key     group item.key     ), poi.noc) - ee((     select sum(soi.noc) sum_1     soi     soi.poi_key = poi.key         , soi.is_shipped = 0     ), :param_2) anon_2 poi 

the model is:

class poi(base):     key = column(integer, primary_key=true)     pok = column(integer, nullable=false)     noc = column(integer, nullable=false)     __table_args__ = (uniqueconstraint(pok,),         foreignkeyconstraint([pok],),{}) 

i'm using scoped_session since application multi-threaded , thought problem. wasn't. i've tried every variation of using session, problem persist. strange thing upon application initialization, when multiple threads started fetch data db, query throws error. invoking query (manually) works fine somehow. problem apparently in interaction other queries.

the error bit vague me, think problem subquery should return 1 result, not many. i'm @ loss start looking answer. threading issue? session issue? else?

i believe don't need from item,poi in 2 subqueries , remove group by in too. regading case expression involving :param_1 not sure if suggest below funcionally correct or not, need create sum() value before can test if sum() null.

select       poi.key     , poi.pok     , poi.noc     , coalesce((             select sum(coalesce(item.noc,:param_1))             item             poi.key = item.poi_key       )       , poi.noc) coalesce_1     , coalesce((             select                   sum(soi.noc) sum_1             soi             soi.poi_key = poi.key                   , soi.is_shipped = 0       )       , @param_2) coalesce_2     , coalesce((             select sum(coalesce(item.noc,:param_1))             item             poi.key = item.poi_key       )       , poi.noc) - ee((             select                   sum(soi.noc) sum_1             soi             soi.poi_key = poi.key                   , soi.is_shipped = 0       )       , :param_2) anon_2 poi 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -