简体   繁体   中英

Sqlalchemy not committing object changes to postgres DB

I am using the following passage of code:

@app.route('/budget_item/<int:budget_id>/edit', methods=['GET', 'POST'])
   def budget_item_edit(budget_id):


budget_item = session.query(Budget).filter_by(id=budget_id).one()
print "Start EDIT sequence"


# Return form data from HTML initial load form
elif request.method == 'POST':

  budget_amount_reallocated_total = budget_item.budget_amount_reallocated_total


  #ORIGINAL BUDGET
  if request.form['transaction_type'] == 'Original Budget':

    #amount
    if request.form['amount'] == "":
      amount = 0
    else:
      amount = float(str(request.form['amount']))

    budget_item = Budget(
      #created_date = "",
      budget_transaction_type = request.form['transaction_type'],
      budget_line = request.form['budget_line'],
      amount = amount,
      description = request.form['description']
      #date_received = request.form['date_received']
      )

    try:
      count = 1

      while count < 10000:
        count += 1

        #budget_line
        setattr(budget_item,'budget_line'+str(count),request.form['budget_line'+str(count)])

        #amount
        setattr(budget_item,'amount'+str(count),float(request.form['amount'+str(count)]))
        budget_amount_reallocated_total += float(request.form['amount'+str(count)])
        setattr(budget_item, 'budget_amount_reallocated_total', budget_amount_reallocated_total)

        #description
        setattr(budget_item,'description'+str(count), request.form['description'+str(count)])

        #date_received
        setattr(budget_item,'date_received'+str(count),request.form['date_received'+str(count)])
        session.commit()

    except:
      session.commit()
      return redirect(url_for('budget_master'))

  else:
    print "I'm done! This is not a post request"

This block of code is setup to pass data from an HTML via a POST request an then update a corresponding object in the Postgres DB. I can confirm that the object queried from the DB "budget_item" is being updated by settattr. At the end of the passage, I use commit() to update the object; however, the database doesn't reflect the changes. Just to test to make sure things are flowing, I've tried session.add(budget_item) followed by session.commit() to make sure the connect to the DB is OK. That works. How do i update this budget_item object into the database? Any help is much appreciated.

i think that a simple

budget_item.budget_amount_reallocated_total = budget_amount_reallocated_total
session.add(budget_item)
session.commit()

is the right way to do it

To answer your question, to update the budget_item that already exists in the database you need to update the Budget instance that you retrieved from the database, ie

budget_item = session.query(Budget).filter_by(id=budget_id).one()

not the one that you have newly created with:

budget_item = Budget(...)

Here the first budget_item represents the row in the database, so this is the one to update. To that end you can replace the code that creates the second Budget instance with this:

budget_item.budget_transaction_type = request.form['transaction_type']
budget_item.budget_line = request.form['budget_line']
budget_item.amount = amount
budget_item.description = request.form['description']

Once you have finished updating the Budget instance you can call session.commit() to flush it to the database.

As mentioned in my comment to your question, it appears that you are trying to add a large number of additional attributes to budget_item all of which will be ignored by sqlalchemy unless they are defined in the mapping between the Budget instance and the Budget table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM