Sqlalchemy query returns Decimal object












0















I have the following model:



  172  class ReportRecord(db.Model):                                                                                                                                                                                
173 __tablename__ = 'tb_report_record' 174 175 id = db.Column(Integer, primary_key=True)
176 report_id = db.Column(ForeignKey('tb_rua_report.id'), index=True)
177 source_ip = db.Column(String(100, 'utf8_bin'))
178 count = db.Column(Integer)
179 disposition = db.Column(String(10, 'utf8_bin'))
180 header_from = db.Column(String(100, 'utf8_bin'))
181 spf_domain = db.Column(String(100, 'utf8_bin'))
182 spf_result = db.Column(String(10, 'utf8_bin'))
183 dkim_domain = db.Column(String(100, 'utf8_bin'))
184 dkim_result = db.Column(String(10, 'utf8_bin'))
185 isActive = db.Column(Integer, nullable=False, server_default=text("'1'")) 186 created_by = db.Column(String(100), nullable=False, server_default=text("'System'"))
187 created_dt = db.Column(DateTime, nullable=False)
188 last_modified_by = db.Column(String(100), nullable=False, server_default=text("'System'"))
189 last_modified_dt = db.Column(DateTime, server_default=text("CURRENT_TIMESTAMP"))
190
191 report = db.relationship('RuaReport', backref=db.backref("record"))


When I am issuing the following query from flasksqlalchemy



ReportRecord.query.filter(or_(ReportRecord.spf_result=="pass", ReportRecord.dkim_result=="pass")).with_entities(func.sum(ReportRecord.count).label('total_passed')).all()


I get the following output:



[(Decimal('930'),)]



Now the value that I get is correct, but I cannot jsonify it directly. Doing a int(Decimal('930')) does give me the desired result but what is the default way to get this data converted in the right format?










share|improve this question



























    0















    I have the following model:



      172  class ReportRecord(db.Model):                                                                                                                                                                                
    173 __tablename__ = 'tb_report_record' 174 175 id = db.Column(Integer, primary_key=True)
    176 report_id = db.Column(ForeignKey('tb_rua_report.id'), index=True)
    177 source_ip = db.Column(String(100, 'utf8_bin'))
    178 count = db.Column(Integer)
    179 disposition = db.Column(String(10, 'utf8_bin'))
    180 header_from = db.Column(String(100, 'utf8_bin'))
    181 spf_domain = db.Column(String(100, 'utf8_bin'))
    182 spf_result = db.Column(String(10, 'utf8_bin'))
    183 dkim_domain = db.Column(String(100, 'utf8_bin'))
    184 dkim_result = db.Column(String(10, 'utf8_bin'))
    185 isActive = db.Column(Integer, nullable=False, server_default=text("'1'")) 186 created_by = db.Column(String(100), nullable=False, server_default=text("'System'"))
    187 created_dt = db.Column(DateTime, nullable=False)
    188 last_modified_by = db.Column(String(100), nullable=False, server_default=text("'System'"))
    189 last_modified_dt = db.Column(DateTime, server_default=text("CURRENT_TIMESTAMP"))
    190
    191 report = db.relationship('RuaReport', backref=db.backref("record"))


    When I am issuing the following query from flasksqlalchemy



    ReportRecord.query.filter(or_(ReportRecord.spf_result=="pass", ReportRecord.dkim_result=="pass")).with_entities(func.sum(ReportRecord.count).label('total_passed')).all()


    I get the following output:



    [(Decimal('930'),)]



    Now the value that I get is correct, but I cannot jsonify it directly. Doing a int(Decimal('930')) does give me the desired result but what is the default way to get this data converted in the right format?










    share|improve this question

























      0












      0








      0








      I have the following model:



        172  class ReportRecord(db.Model):                                                                                                                                                                                
      173 __tablename__ = 'tb_report_record' 174 175 id = db.Column(Integer, primary_key=True)
      176 report_id = db.Column(ForeignKey('tb_rua_report.id'), index=True)
      177 source_ip = db.Column(String(100, 'utf8_bin'))
      178 count = db.Column(Integer)
      179 disposition = db.Column(String(10, 'utf8_bin'))
      180 header_from = db.Column(String(100, 'utf8_bin'))
      181 spf_domain = db.Column(String(100, 'utf8_bin'))
      182 spf_result = db.Column(String(10, 'utf8_bin'))
      183 dkim_domain = db.Column(String(100, 'utf8_bin'))
      184 dkim_result = db.Column(String(10, 'utf8_bin'))
      185 isActive = db.Column(Integer, nullable=False, server_default=text("'1'")) 186 created_by = db.Column(String(100), nullable=False, server_default=text("'System'"))
      187 created_dt = db.Column(DateTime, nullable=False)
      188 last_modified_by = db.Column(String(100), nullable=False, server_default=text("'System'"))
      189 last_modified_dt = db.Column(DateTime, server_default=text("CURRENT_TIMESTAMP"))
      190
      191 report = db.relationship('RuaReport', backref=db.backref("record"))


      When I am issuing the following query from flasksqlalchemy



      ReportRecord.query.filter(or_(ReportRecord.spf_result=="pass", ReportRecord.dkim_result=="pass")).with_entities(func.sum(ReportRecord.count).label('total_passed')).all()


      I get the following output:



      [(Decimal('930'),)]



      Now the value that I get is correct, but I cannot jsonify it directly. Doing a int(Decimal('930')) does give me the desired result but what is the default way to get this data converted in the right format?










      share|improve this question














      I have the following model:



        172  class ReportRecord(db.Model):                                                                                                                                                                                
      173 __tablename__ = 'tb_report_record' 174 175 id = db.Column(Integer, primary_key=True)
      176 report_id = db.Column(ForeignKey('tb_rua_report.id'), index=True)
      177 source_ip = db.Column(String(100, 'utf8_bin'))
      178 count = db.Column(Integer)
      179 disposition = db.Column(String(10, 'utf8_bin'))
      180 header_from = db.Column(String(100, 'utf8_bin'))
      181 spf_domain = db.Column(String(100, 'utf8_bin'))
      182 spf_result = db.Column(String(10, 'utf8_bin'))
      183 dkim_domain = db.Column(String(100, 'utf8_bin'))
      184 dkim_result = db.Column(String(10, 'utf8_bin'))
      185 isActive = db.Column(Integer, nullable=False, server_default=text("'1'")) 186 created_by = db.Column(String(100), nullable=False, server_default=text("'System'"))
      187 created_dt = db.Column(DateTime, nullable=False)
      188 last_modified_by = db.Column(String(100), nullable=False, server_default=text("'System'"))
      189 last_modified_dt = db.Column(DateTime, server_default=text("CURRENT_TIMESTAMP"))
      190
      191 report = db.relationship('RuaReport', backref=db.backref("record"))


      When I am issuing the following query from flasksqlalchemy



      ReportRecord.query.filter(or_(ReportRecord.spf_result=="pass", ReportRecord.dkim_result=="pass")).with_entities(func.sum(ReportRecord.count).label('total_passed')).all()


      I get the following output:



      [(Decimal('930'),)]



      Now the value that I get is correct, but I cannot jsonify it directly. Doing a int(Decimal('930')) does give me the desired result but what is the default way to get this data converted in the right format?







      casting sqlalchemy flask-sqlalchemy sqldatatypes






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked May 18 '18 at 10:34









      Ishan KhareIshan Khare

      655928




      655928
























          2 Answers
          2






          active

          oldest

          votes


















          1














          How about converting the decimal while querying it?



          Try int(func.sum(ReportRecord.count).label('total_passed'))



          try enforcing your db to cast values into integer should solve your problem



          I cannot check but there is a cast function which might be useful as well



          from sqlalchemy.sql.expression import cast
          cast(func.sum(ReportRecord.count).label('total_passed'),sqlalchemy.Integer)





          share|improve this answer































            1














            This worked for me:
            Include this in your imports:



            from sqlalchemy import func


            then:



            total_passed = db.session.query(func.sum(ReportRecord.count)).scalar()


            This returns 930 instead of [(Decimal('930'),)]



            Read more about .scalar()






            share|improve this answer























              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%2f50409403%2fsqlalchemy-query-returns-decimal-object%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              How about converting the decimal while querying it?



              Try int(func.sum(ReportRecord.count).label('total_passed'))



              try enforcing your db to cast values into integer should solve your problem



              I cannot check but there is a cast function which might be useful as well



              from sqlalchemy.sql.expression import cast
              cast(func.sum(ReportRecord.count).label('total_passed'),sqlalchemy.Integer)





              share|improve this answer




























                1














                How about converting the decimal while querying it?



                Try int(func.sum(ReportRecord.count).label('total_passed'))



                try enforcing your db to cast values into integer should solve your problem



                I cannot check but there is a cast function which might be useful as well



                from sqlalchemy.sql.expression import cast
                cast(func.sum(ReportRecord.count).label('total_passed'),sqlalchemy.Integer)





                share|improve this answer


























                  1












                  1








                  1







                  How about converting the decimal while querying it?



                  Try int(func.sum(ReportRecord.count).label('total_passed'))



                  try enforcing your db to cast values into integer should solve your problem



                  I cannot check but there is a cast function which might be useful as well



                  from sqlalchemy.sql.expression import cast
                  cast(func.sum(ReportRecord.count).label('total_passed'),sqlalchemy.Integer)





                  share|improve this answer













                  How about converting the decimal while querying it?



                  Try int(func.sum(ReportRecord.count).label('total_passed'))



                  try enforcing your db to cast values into integer should solve your problem



                  I cannot check but there is a cast function which might be useful as well



                  from sqlalchemy.sql.expression import cast
                  cast(func.sum(ReportRecord.count).label('total_passed'),sqlalchemy.Integer)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 18 '18 at 16:01









                  mad_mad_

                  4,12011022




                  4,12011022

























                      1














                      This worked for me:
                      Include this in your imports:



                      from sqlalchemy import func


                      then:



                      total_passed = db.session.query(func.sum(ReportRecord.count)).scalar()


                      This returns 930 instead of [(Decimal('930'),)]



                      Read more about .scalar()






                      share|improve this answer




























                        1














                        This worked for me:
                        Include this in your imports:



                        from sqlalchemy import func


                        then:



                        total_passed = db.session.query(func.sum(ReportRecord.count)).scalar()


                        This returns 930 instead of [(Decimal('930'),)]



                        Read more about .scalar()






                        share|improve this answer


























                          1












                          1








                          1







                          This worked for me:
                          Include this in your imports:



                          from sqlalchemy import func


                          then:



                          total_passed = db.session.query(func.sum(ReportRecord.count)).scalar()


                          This returns 930 instead of [(Decimal('930'),)]



                          Read more about .scalar()






                          share|improve this answer













                          This worked for me:
                          Include this in your imports:



                          from sqlalchemy import func


                          then:



                          total_passed = db.session.query(func.sum(ReportRecord.count)).scalar()


                          This returns 930 instead of [(Decimal('930'),)]



                          Read more about .scalar()







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 22 '18 at 13:06









                          Eric O.Eric O.

                          549




                          549






























                              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%2f50409403%2fsqlalchemy-query-returns-decimal-object%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

                              Wiesbaden

                              Marschland

                              Dieringhausen