sqlalchemy flask ignoring an and_ and func.DATE in filter query










0















I have this query that is trying to find a record given the same day and a status:



ld=LunchDay.query.filter(and_(func.DATE(LunchDay.timestamp == datetime.date.today()), LunchDay.status==1))


The model:



class LunchDay(db.Model):
__tablename__ = 'lunch_day'

id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
user = relationship("User", foreign_keys=[user_id])
timestamp = db.Column(db.DateTime, index=True, default=datetime.today())
status = db.Column(db.Integer)


It runs and doesn't throw an error, yet it seems to not regard date. It will find entries with dates like yesterdays in the DateTimeField of the database:



2018-11-13 00:00:00.000000


Which is yesterdays date, but it is picking it up based just on the status almost just like it is looking at it as an OR. The imports I use are:



from sqlalchemy import func, and_









share|improve this question




























    0















    I have this query that is trying to find a record given the same day and a status:



    ld=LunchDay.query.filter(and_(func.DATE(LunchDay.timestamp == datetime.date.today()), LunchDay.status==1))


    The model:



    class LunchDay(db.Model):
    __tablename__ = 'lunch_day'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    user = relationship("User", foreign_keys=[user_id])
    timestamp = db.Column(db.DateTime, index=True, default=datetime.today())
    status = db.Column(db.Integer)


    It runs and doesn't throw an error, yet it seems to not regard date. It will find entries with dates like yesterdays in the DateTimeField of the database:



    2018-11-13 00:00:00.000000


    Which is yesterdays date, but it is picking it up based just on the status almost just like it is looking at it as an OR. The imports I use are:



    from sqlalchemy import func, and_









    share|improve this question


























      0












      0








      0








      I have this query that is trying to find a record given the same day and a status:



      ld=LunchDay.query.filter(and_(func.DATE(LunchDay.timestamp == datetime.date.today()), LunchDay.status==1))


      The model:



      class LunchDay(db.Model):
      __tablename__ = 'lunch_day'

      id = db.Column(db.Integer, primary_key=True)
      user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
      user = relationship("User", foreign_keys=[user_id])
      timestamp = db.Column(db.DateTime, index=True, default=datetime.today())
      status = db.Column(db.Integer)


      It runs and doesn't throw an error, yet it seems to not regard date. It will find entries with dates like yesterdays in the DateTimeField of the database:



      2018-11-13 00:00:00.000000


      Which is yesterdays date, but it is picking it up based just on the status almost just like it is looking at it as an OR. The imports I use are:



      from sqlalchemy import func, and_









      share|improve this question
















      I have this query that is trying to find a record given the same day and a status:



      ld=LunchDay.query.filter(and_(func.DATE(LunchDay.timestamp == datetime.date.today()), LunchDay.status==1))


      The model:



      class LunchDay(db.Model):
      __tablename__ = 'lunch_day'

      id = db.Column(db.Integer, primary_key=True)
      user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
      user = relationship("User", foreign_keys=[user_id])
      timestamp = db.Column(db.DateTime, index=True, default=datetime.today())
      status = db.Column(db.Integer)


      It runs and doesn't throw an error, yet it seems to not regard date. It will find entries with dates like yesterdays in the DateTimeField of the database:



      2018-11-13 00:00:00.000000


      Which is yesterdays date, but it is picking it up based just on the status almost just like it is looking at it as an OR. The imports I use are:



      from sqlalchemy import func, and_






      flask sqlalchemy flask-sqlalchemy






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 20:24







      Codejoy

















      asked Nov 14 '18 at 21:33









      CodejoyCodejoy

      1,31793868




      1,31793868






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Print out the actual sql that is being generated by your query to see what is happening. E.g.:



          ld=LunchDay.query.filter(and_(func.DATE(LunchDay.timestamp == datetime.date.today()), LunchDay.status==1))
          print(ld)


          Prints:



          SELECT lunch_day.id AS lunch_day_id, lunch_day.timestamp AS lunch_day_timestamp, lunch_day.status AS lunch_day_status
          FROM lunch_day
          WHERE DATE(lunch_day.timestamp = %(timestamp_1)s) AND lunch_day.status = %(status_1)s


          There you can see that the equality of lunch_day.timestamp and the param timestamp_1 is being passed to the DATE function.



          Which is actually pretty easy to see in your sqlalchemy query: func.DATE(LunchDay.timestamp == datetime.date.today()). I assume you want to convert LunchDay.timestamp to a date and then compare it to datetime.date.today() which should be db.func.DATE(LunchDay.timestamp) == date.today().



          print(LunchDay.query.filter(and_(func.DATE(LunchDay.timestamp) == datetime.date.today(), LunchDay.status == 1)))


          prints:



          SELECT lunch_day.id AS lunch_day_id, lunch_day.timestamp AS lunch_day_timestamp, lunch_day.status AS lunch_day_status
          FROM lunch_day
          WHERE DATE(lunch_day.timestamp) = %(DATE_1)s AND lunch_day.status = %(status_1)s


          One other thing to note is that multiple statements passed to filter() are automatically treated as an and_(), so you can simplify your query a little by removing that:



          LunchDay.query.filter(func.DATE(LunchDay.timestamp) == datetime.date.today(), LunchDay.status == 1)





          share|improve this answer

























          • This did it, my parenthesis was just in the wrong spot and thanks for the extra info too! awesome!

            – Codejoy
            Nov 21 '18 at 19:22










          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%2f53309052%2fsqlalchemy-flask-ignoring-an-and-and-func-date-in-filter-query%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Print out the actual sql that is being generated by your query to see what is happening. E.g.:



          ld=LunchDay.query.filter(and_(func.DATE(LunchDay.timestamp == datetime.date.today()), LunchDay.status==1))
          print(ld)


          Prints:



          SELECT lunch_day.id AS lunch_day_id, lunch_day.timestamp AS lunch_day_timestamp, lunch_day.status AS lunch_day_status
          FROM lunch_day
          WHERE DATE(lunch_day.timestamp = %(timestamp_1)s) AND lunch_day.status = %(status_1)s


          There you can see that the equality of lunch_day.timestamp and the param timestamp_1 is being passed to the DATE function.



          Which is actually pretty easy to see in your sqlalchemy query: func.DATE(LunchDay.timestamp == datetime.date.today()). I assume you want to convert LunchDay.timestamp to a date and then compare it to datetime.date.today() which should be db.func.DATE(LunchDay.timestamp) == date.today().



          print(LunchDay.query.filter(and_(func.DATE(LunchDay.timestamp) == datetime.date.today(), LunchDay.status == 1)))


          prints:



          SELECT lunch_day.id AS lunch_day_id, lunch_day.timestamp AS lunch_day_timestamp, lunch_day.status AS lunch_day_status
          FROM lunch_day
          WHERE DATE(lunch_day.timestamp) = %(DATE_1)s AND lunch_day.status = %(status_1)s


          One other thing to note is that multiple statements passed to filter() are automatically treated as an and_(), so you can simplify your query a little by removing that:



          LunchDay.query.filter(func.DATE(LunchDay.timestamp) == datetime.date.today(), LunchDay.status == 1)





          share|improve this answer

























          • This did it, my parenthesis was just in the wrong spot and thanks for the extra info too! awesome!

            – Codejoy
            Nov 21 '18 at 19:22















          1














          Print out the actual sql that is being generated by your query to see what is happening. E.g.:



          ld=LunchDay.query.filter(and_(func.DATE(LunchDay.timestamp == datetime.date.today()), LunchDay.status==1))
          print(ld)


          Prints:



          SELECT lunch_day.id AS lunch_day_id, lunch_day.timestamp AS lunch_day_timestamp, lunch_day.status AS lunch_day_status
          FROM lunch_day
          WHERE DATE(lunch_day.timestamp = %(timestamp_1)s) AND lunch_day.status = %(status_1)s


          There you can see that the equality of lunch_day.timestamp and the param timestamp_1 is being passed to the DATE function.



          Which is actually pretty easy to see in your sqlalchemy query: func.DATE(LunchDay.timestamp == datetime.date.today()). I assume you want to convert LunchDay.timestamp to a date and then compare it to datetime.date.today() which should be db.func.DATE(LunchDay.timestamp) == date.today().



          print(LunchDay.query.filter(and_(func.DATE(LunchDay.timestamp) == datetime.date.today(), LunchDay.status == 1)))


          prints:



          SELECT lunch_day.id AS lunch_day_id, lunch_day.timestamp AS lunch_day_timestamp, lunch_day.status AS lunch_day_status
          FROM lunch_day
          WHERE DATE(lunch_day.timestamp) = %(DATE_1)s AND lunch_day.status = %(status_1)s


          One other thing to note is that multiple statements passed to filter() are automatically treated as an and_(), so you can simplify your query a little by removing that:



          LunchDay.query.filter(func.DATE(LunchDay.timestamp) == datetime.date.today(), LunchDay.status == 1)





          share|improve this answer

























          • This did it, my parenthesis was just in the wrong spot and thanks for the extra info too! awesome!

            – Codejoy
            Nov 21 '18 at 19:22













          1












          1








          1







          Print out the actual sql that is being generated by your query to see what is happening. E.g.:



          ld=LunchDay.query.filter(and_(func.DATE(LunchDay.timestamp == datetime.date.today()), LunchDay.status==1))
          print(ld)


          Prints:



          SELECT lunch_day.id AS lunch_day_id, lunch_day.timestamp AS lunch_day_timestamp, lunch_day.status AS lunch_day_status
          FROM lunch_day
          WHERE DATE(lunch_day.timestamp = %(timestamp_1)s) AND lunch_day.status = %(status_1)s


          There you can see that the equality of lunch_day.timestamp and the param timestamp_1 is being passed to the DATE function.



          Which is actually pretty easy to see in your sqlalchemy query: func.DATE(LunchDay.timestamp == datetime.date.today()). I assume you want to convert LunchDay.timestamp to a date and then compare it to datetime.date.today() which should be db.func.DATE(LunchDay.timestamp) == date.today().



          print(LunchDay.query.filter(and_(func.DATE(LunchDay.timestamp) == datetime.date.today(), LunchDay.status == 1)))


          prints:



          SELECT lunch_day.id AS lunch_day_id, lunch_day.timestamp AS lunch_day_timestamp, lunch_day.status AS lunch_day_status
          FROM lunch_day
          WHERE DATE(lunch_day.timestamp) = %(DATE_1)s AND lunch_day.status = %(status_1)s


          One other thing to note is that multiple statements passed to filter() are automatically treated as an and_(), so you can simplify your query a little by removing that:



          LunchDay.query.filter(func.DATE(LunchDay.timestamp) == datetime.date.today(), LunchDay.status == 1)





          share|improve this answer















          Print out the actual sql that is being generated by your query to see what is happening. E.g.:



          ld=LunchDay.query.filter(and_(func.DATE(LunchDay.timestamp == datetime.date.today()), LunchDay.status==1))
          print(ld)


          Prints:



          SELECT lunch_day.id AS lunch_day_id, lunch_day.timestamp AS lunch_day_timestamp, lunch_day.status AS lunch_day_status
          FROM lunch_day
          WHERE DATE(lunch_day.timestamp = %(timestamp_1)s) AND lunch_day.status = %(status_1)s


          There you can see that the equality of lunch_day.timestamp and the param timestamp_1 is being passed to the DATE function.



          Which is actually pretty easy to see in your sqlalchemy query: func.DATE(LunchDay.timestamp == datetime.date.today()). I assume you want to convert LunchDay.timestamp to a date and then compare it to datetime.date.today() which should be db.func.DATE(LunchDay.timestamp) == date.today().



          print(LunchDay.query.filter(and_(func.DATE(LunchDay.timestamp) == datetime.date.today(), LunchDay.status == 1)))


          prints:



          SELECT lunch_day.id AS lunch_day_id, lunch_day.timestamp AS lunch_day_timestamp, lunch_day.status AS lunch_day_status
          FROM lunch_day
          WHERE DATE(lunch_day.timestamp) = %(DATE_1)s AND lunch_day.status = %(status_1)s


          One other thing to note is that multiple statements passed to filter() are automatically treated as an and_(), so you can simplify your query a little by removing that:



          LunchDay.query.filter(func.DATE(LunchDay.timestamp) == datetime.date.today(), LunchDay.status == 1)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 '18 at 23:40

























          answered Nov 19 '18 at 23:27









          SuperShootSuperShoot

          1,921921




          1,921921












          • This did it, my parenthesis was just in the wrong spot and thanks for the extra info too! awesome!

            – Codejoy
            Nov 21 '18 at 19:22

















          • This did it, my parenthesis was just in the wrong spot and thanks for the extra info too! awesome!

            – Codejoy
            Nov 21 '18 at 19:22
















          This did it, my parenthesis was just in the wrong spot and thanks for the extra info too! awesome!

          – Codejoy
          Nov 21 '18 at 19:22





          This did it, my parenthesis was just in the wrong spot and thanks for the extra info too! awesome!

          – Codejoy
          Nov 21 '18 at 19:22



















          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%2f53309052%2fsqlalchemy-flask-ignoring-an-and-and-func-date-in-filter-query%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

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo