title: A Flask-SQLAlchemy update is creating a new record in MySQL
body: <p>I am trying to update a MySQL record from flask-sqlalchemy, but instead of updating the current record, it is adding a new one. But here's the really weird thing, when I run the same code from the Python prompt it updates properly without adding a new record.</p>
  
  <p>Here is the relevant portion of the code:</p>
  
  <p>First, the template that feeds the blog:</p>
  
  <pre><code>{% for post in posts %}
    &lt;article class="blog"&gt;
      &lt;header&gt;
        &lt;h2&gt;{{ post.title|safe }}&lt;/h2&gt;
      &lt;/header&gt;
      &lt;main&gt;
        {{ post.content|safe }}
      &lt;/main&gt;
      &lt;footer&gt;
        &lt;p&gt;{{ post.posted.strftime('%d %B %Y %I:%M')|safe }}&lt;p&gt;
        {% if session.logged_in %}
          &lt;form action="{{ url_for('blog') }}" method="post"&gt;
            &lt;div style="display:none;"&gt;
              &lt;input type="hidden" name="blog_id" value="{{ post.id|safe }}"&gt;
            &lt;/div&gt;
  
            &lt;div class="form-group submit"&gt;
              &lt;input type="submit" name="submit" class="btn" value="Edit Post"&gt;
              &lt;input type="submit" name="submit" class="btn" value="Delete Post"&gt;
            &lt;/div&gt;
          &lt;/form&gt;
        {% endif %}
      &lt;/footer&gt;
    &lt;/article&gt;
  {% endfor %}
  </code></pre>
  
  <p>Now, the code for the views:</p>
  
  <pre><code>@app.route('/blog', methods = ['GET', 'POST'])
  def blog():
      # Get all records from database blog table:
      posts = models.Blog.query.order_by(models.Blog.posted.desc()).all()
      context = {
          'copyright': COPYRIGHT,
          'posts': posts
      }
      if request.method == 'POST':
          blog_id = request.form.get('blog_id')
          if request.form.get('submit') == 'Edit Post':
              return redirect(url_for('edit_entry', blog_id = blog_id))
          elif request.form.get('submit') == 'Delete Post':
              pass
      elif request.method == 'GET':
          return render_template('blog.html', **context)
  
  @app.route('/edit_entry', methods = ['GET', 'POST'])
  def edit_entry():
      form = BlogEntry() # A WTForms object.
      # blog_id is passed from a redirect. Tested and ensured it is passing proper value.
      blog_id = request.args['blog_id']
      post = models.Blog.query.filter_by(id = blog_id).one()
      if request.method == 'POST':
          post.title = form.title.data
          post.content = form.content.data
          db.session.commit()
          return redirect(url_for('blog'))
      elif request.method == 'GET':
          context = {
              'copyright': COPYRIGHT,
              'form': form,
          }
          form.title.data = post.title
          form.content.data = post.content
          return render_template('edit_entry.html', **context)
  </code></pre>
  
  <p>This is the code as it is right now in my app file. I've run the exact same code from the Python prompt in the terminal, and it works perfectly, but when this code runs from the app file, it creates a new record instead of updating the existing one.</p>
  
  <p>As a note, I have also tried:</p>
  
  <pre><code>models.Blog.query.filter_by(id=blog_id).update({
      'title': form.title.data,
      'content': form.content.data
  })
  db.session.commit()
  </code></pre>
  
  <p>and by replacing models.Blog with db.session.query(models.Blog). Basically, i've tried every method and combination of code for updating records using flask-sqlalchemy that I could find on this forums and others. All the same issue. When I run them from the python prompt, they update the correct record in the database. When I run them from the app, they create a new record.</p>
  
  <p>If you want to see more of the code, you can check the entire project here: <a href="https://github.com/tbellerose/lauraopfelphotography.com" rel="nofollow">https://github.com/tbellerose/lauraopfelphotography.com</a></p>