Problems with Sqlachemy. Can't understand the difference











up vote
0
down vote

favorite












I have the following code



def update(project_id, code, description):

if project_id is None:
raise exception

with session_handler() as session:
project = session.query(Project).filter_by(project_id=project_id).first()
project_upd = session.query(Project).filter_by(project_id=project_id)

if project is None:
raise ProjectDoesntExist(f"Project {project_id} does not exist.")
data = _build_update_data(code, description)

if not data:
raise ValueError("No code or description provided")

project_upd.update(data)


So if I replace project_upd.update(data) with project.update(data) it gives the following error




Attribute Error: Project object has no attribute update.




How I can use only one variable?










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have the following code



    def update(project_id, code, description):

    if project_id is None:
    raise exception

    with session_handler() as session:
    project = session.query(Project).filter_by(project_id=project_id).first()
    project_upd = session.query(Project).filter_by(project_id=project_id)

    if project is None:
    raise ProjectDoesntExist(f"Project {project_id} does not exist.")
    data = _build_update_data(code, description)

    if not data:
    raise ValueError("No code or description provided")

    project_upd.update(data)


    So if I replace project_upd.update(data) with project.update(data) it gives the following error




    Attribute Error: Project object has no attribute update.




    How I can use only one variable?










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have the following code



      def update(project_id, code, description):

      if project_id is None:
      raise exception

      with session_handler() as session:
      project = session.query(Project).filter_by(project_id=project_id).first()
      project_upd = session.query(Project).filter_by(project_id=project_id)

      if project is None:
      raise ProjectDoesntExist(f"Project {project_id} does not exist.")
      data = _build_update_data(code, description)

      if not data:
      raise ValueError("No code or description provided")

      project_upd.update(data)


      So if I replace project_upd.update(data) with project.update(data) it gives the following error




      Attribute Error: Project object has no attribute update.




      How I can use only one variable?










      share|improve this question















      I have the following code



      def update(project_id, code, description):

      if project_id is None:
      raise exception

      with session_handler() as session:
      project = session.query(Project).filter_by(project_id=project_id).first()
      project_upd = session.query(Project).filter_by(project_id=project_id)

      if project is None:
      raise ProjectDoesntExist(f"Project {project_id} does not exist.")
      data = _build_update_data(code, description)

      if not data:
      raise ValueError("No code or description provided")

      project_upd.update(data)


      So if I replace project_upd.update(data) with project.update(data) it gives the following error




      Attribute Error: Project object has no attribute update.




      How I can use only one variable?







      python sql sqlalchemy






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 8:02









      Ilja Everilä

      22.4k33459




      22.4k33459










      asked Nov 8 at 7:20









      Mila Bogomolova

      11




      11
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          Though the Project model is omitted, it is clear that it has no method update() – which is the norm. The difference is that project is bound to a Project object, and project_upd to a Query object. In other words the former represents a single row in the table mapped to an object, while the latter represents a query against that table.



          Your options are:




          1. Issue an UPDATE statement only and check if it matched any rows, raise if not.

          2. Fetch the row / object for update, check if it existed and raise if not, do the update.


          I assume that session_handler() commits, if no exception was raised. If that is not the case, add an explicit session.commit() as necessary.



          1. Single UPDATE statement



          def update(project_id, code, description):
          if project_id is None:
          raise exception

          data = _build_update_data(code, description)

          if not data:
          raise ValueError("No code or description provided")

          with session_handler() as session:
          row_count = session.query(Project).
          filter_by(project_id=project_id).
          update(data)

          if not row_count:
          raise ProjectDoesntExist(f"Project {project_id} does not exist.")


          2. Fetch and update



          def update(project_id, code, description):
          if project_id is None:
          raise exception

          data = _build_update_data(code, description)

          if not data:
          raise ValueError("No code or description provided")

          with session_handler() as session:
          # Fetch FOR UPDATE so that no concurrent updates may proceed in between
          # getting the `Project` instance and actually updating it.
          project = session.query(Project).
          filter_by(project_id=project_id).
          with_for_update().
          first()

          if project is None:
          raise ProjectDoesntExist(f"Project {project_id} does not exist.")

          for attr, val in data.items():
          setattr(project, attr, val)


          Locking with FOR UPDATE might be a bit unnecessary in this case, since the new values don't seem to depend on previous state of the object. Still, it is something to keep in mind.






          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',
            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%2f53203039%2fproblems-with-sqlachemy-cant-understand-the-difference%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








            up vote
            2
            down vote













            Though the Project model is omitted, it is clear that it has no method update() – which is the norm. The difference is that project is bound to a Project object, and project_upd to a Query object. In other words the former represents a single row in the table mapped to an object, while the latter represents a query against that table.



            Your options are:




            1. Issue an UPDATE statement only and check if it matched any rows, raise if not.

            2. Fetch the row / object for update, check if it existed and raise if not, do the update.


            I assume that session_handler() commits, if no exception was raised. If that is not the case, add an explicit session.commit() as necessary.



            1. Single UPDATE statement



            def update(project_id, code, description):
            if project_id is None:
            raise exception

            data = _build_update_data(code, description)

            if not data:
            raise ValueError("No code or description provided")

            with session_handler() as session:
            row_count = session.query(Project).
            filter_by(project_id=project_id).
            update(data)

            if not row_count:
            raise ProjectDoesntExist(f"Project {project_id} does not exist.")


            2. Fetch and update



            def update(project_id, code, description):
            if project_id is None:
            raise exception

            data = _build_update_data(code, description)

            if not data:
            raise ValueError("No code or description provided")

            with session_handler() as session:
            # Fetch FOR UPDATE so that no concurrent updates may proceed in between
            # getting the `Project` instance and actually updating it.
            project = session.query(Project).
            filter_by(project_id=project_id).
            with_for_update().
            first()

            if project is None:
            raise ProjectDoesntExist(f"Project {project_id} does not exist.")

            for attr, val in data.items():
            setattr(project, attr, val)


            Locking with FOR UPDATE might be a bit unnecessary in this case, since the new values don't seem to depend on previous state of the object. Still, it is something to keep in mind.






            share|improve this answer



























              up vote
              2
              down vote













              Though the Project model is omitted, it is clear that it has no method update() – which is the norm. The difference is that project is bound to a Project object, and project_upd to a Query object. In other words the former represents a single row in the table mapped to an object, while the latter represents a query against that table.



              Your options are:




              1. Issue an UPDATE statement only and check if it matched any rows, raise if not.

              2. Fetch the row / object for update, check if it existed and raise if not, do the update.


              I assume that session_handler() commits, if no exception was raised. If that is not the case, add an explicit session.commit() as necessary.



              1. Single UPDATE statement



              def update(project_id, code, description):
              if project_id is None:
              raise exception

              data = _build_update_data(code, description)

              if not data:
              raise ValueError("No code or description provided")

              with session_handler() as session:
              row_count = session.query(Project).
              filter_by(project_id=project_id).
              update(data)

              if not row_count:
              raise ProjectDoesntExist(f"Project {project_id} does not exist.")


              2. Fetch and update



              def update(project_id, code, description):
              if project_id is None:
              raise exception

              data = _build_update_data(code, description)

              if not data:
              raise ValueError("No code or description provided")

              with session_handler() as session:
              # Fetch FOR UPDATE so that no concurrent updates may proceed in between
              # getting the `Project` instance and actually updating it.
              project = session.query(Project).
              filter_by(project_id=project_id).
              with_for_update().
              first()

              if project is None:
              raise ProjectDoesntExist(f"Project {project_id} does not exist.")

              for attr, val in data.items():
              setattr(project, attr, val)


              Locking with FOR UPDATE might be a bit unnecessary in this case, since the new values don't seem to depend on previous state of the object. Still, it is something to keep in mind.






              share|improve this answer

























                up vote
                2
                down vote










                up vote
                2
                down vote









                Though the Project model is omitted, it is clear that it has no method update() – which is the norm. The difference is that project is bound to a Project object, and project_upd to a Query object. In other words the former represents a single row in the table mapped to an object, while the latter represents a query against that table.



                Your options are:




                1. Issue an UPDATE statement only and check if it matched any rows, raise if not.

                2. Fetch the row / object for update, check if it existed and raise if not, do the update.


                I assume that session_handler() commits, if no exception was raised. If that is not the case, add an explicit session.commit() as necessary.



                1. Single UPDATE statement



                def update(project_id, code, description):
                if project_id is None:
                raise exception

                data = _build_update_data(code, description)

                if not data:
                raise ValueError("No code or description provided")

                with session_handler() as session:
                row_count = session.query(Project).
                filter_by(project_id=project_id).
                update(data)

                if not row_count:
                raise ProjectDoesntExist(f"Project {project_id} does not exist.")


                2. Fetch and update



                def update(project_id, code, description):
                if project_id is None:
                raise exception

                data = _build_update_data(code, description)

                if not data:
                raise ValueError("No code or description provided")

                with session_handler() as session:
                # Fetch FOR UPDATE so that no concurrent updates may proceed in between
                # getting the `Project` instance and actually updating it.
                project = session.query(Project).
                filter_by(project_id=project_id).
                with_for_update().
                first()

                if project is None:
                raise ProjectDoesntExist(f"Project {project_id} does not exist.")

                for attr, val in data.items():
                setattr(project, attr, val)


                Locking with FOR UPDATE might be a bit unnecessary in this case, since the new values don't seem to depend on previous state of the object. Still, it is something to keep in mind.






                share|improve this answer














                Though the Project model is omitted, it is clear that it has no method update() – which is the norm. The difference is that project is bound to a Project object, and project_upd to a Query object. In other words the former represents a single row in the table mapped to an object, while the latter represents a query against that table.



                Your options are:




                1. Issue an UPDATE statement only and check if it matched any rows, raise if not.

                2. Fetch the row / object for update, check if it existed and raise if not, do the update.


                I assume that session_handler() commits, if no exception was raised. If that is not the case, add an explicit session.commit() as necessary.



                1. Single UPDATE statement



                def update(project_id, code, description):
                if project_id is None:
                raise exception

                data = _build_update_data(code, description)

                if not data:
                raise ValueError("No code or description provided")

                with session_handler() as session:
                row_count = session.query(Project).
                filter_by(project_id=project_id).
                update(data)

                if not row_count:
                raise ProjectDoesntExist(f"Project {project_id} does not exist.")


                2. Fetch and update



                def update(project_id, code, description):
                if project_id is None:
                raise exception

                data = _build_update_data(code, description)

                if not data:
                raise ValueError("No code or description provided")

                with session_handler() as session:
                # Fetch FOR UPDATE so that no concurrent updates may proceed in between
                # getting the `Project` instance and actually updating it.
                project = session.query(Project).
                filter_by(project_id=project_id).
                with_for_update().
                first()

                if project is None:
                raise ProjectDoesntExist(f"Project {project_id} does not exist.")

                for attr, val in data.items():
                setattr(project, attr, val)


                Locking with FOR UPDATE might be a bit unnecessary in this case, since the new values don't seem to depend on previous state of the object. Still, it is something to keep in mind.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 8 at 12:31

























                answered Nov 8 at 11:26









                Ilja Everilä

                22.4k33459




                22.4k33459






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203039%2fproblems-with-sqlachemy-cant-understand-the-difference%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

                    Schultheiß

                    Verwaltungsgliederung Dänemarks

                    Liste der Kulturdenkmale in Wilsdruff