Flask-Sqlalchemy Update a Row's Information

Flask-SQLalchemy update a row's information

Retrieve an object using the tutorial shown in the Flask-SQLAlchemy documentation. Once you have the entity that you want to change, change the entity itself. Then, db.session.commit().

For example:

admin = User.query.filter_by(username='admin').first()
admin.email = 'my_new_email@example.com'
db.session.commit()

user = User.query.get(5)
user.name = 'New Name'
db.session.commit()

Flask-SQLAlchemy is based on SQLAlchemy, so be sure to check out the SQLAlchemy Docs as well.

flask_sqlalchemy UPDATE row details not updating mySQL database

After struggling I found out where I was wrong, someone else facing the issue can try the following thing.

What worked for me -

usr.password = data['new_password']
db.session.merge(usr)
db.session.commit()

Things that did not work for me despite having them as answer on some of the stackoverflow questions -

usr.data["password"] = data['new_password']
db.session.commit()
--------------------------------------------
usr.password = data['new_password']
db.session.add(usr)
db.session.commit()
--------------------------------------------
usr.data = { "password": data['new_password'] }
db.session.commit()

Hope this might help you!

Updating a specific row with Flask SQLAlchemy

solved it by adding:

myParent = db.session.merge(myParent)

this way it merges the current session with the previous one. It still returns a 302 but the data on the db has been successfully updated.

How to update row using Flask-SQLAlchemy?

First get the item you need.

item = Announcements.query.get(id)

Then you edit that item directly:

item.name = "Newname"

Then you commit the session:

db.session.commit()

How to update SQLAlchemy row entry?

user.no_of_logins += 1
session.commit()

flask sqlalchemy doesn't update rows

You could try:

db.session.merge(x)
db.session.commit()

This code creates a new row, if it did not exist, else updates the existing one.

easier method to update SQLAlchemy object from form data

You might want to take a look at flask-wtf's populate_obj function. Furthermore, the given example shows you how to fill the form by passing the requested database object to your form using the obj argument.

@app.route('/edit/<int:post_id>', methods=['GET', 'POST'])
def edit_post(post_id):
post = BlogPost.query.get_or_404(post_id)
form = CreatePostForm(request.form, obj=post)
if form.validate_on_submit():
form.populate_obj(post)
db.session.commit()
return redirect(url_for('show_post', index=post_id))
return render_template('make-post.html', **locals())

How to update a Flask-SQLAlchemy one to many relationship

To anyone in the future who stumbles upon this question while researching, this is how I solved it:

        #delete current data
Religion.query.filter(Religion.user_id == current_user.id).delete()
Size.query.filter(Size.user_id == current_user.id).delete()
Major.query.filter(Major.user_id == current_user.id).delete()
Setting.query.filter(Setting.user_id == current_user.id).delete()
Region.query.filter(Region.user_id == current_user.id).delete()
State.query.filter(State.user_id == current_user.id).delete()
SpecPref.query.filter(SpecPref.user_id == current_user.id).delete()

#now add all of the new data
db.session.add_all([Religion(code=rel,name=dict(religionChoices).get(rel),user_id = current_user.id) for rel in form.relAffil.data])
db.session.add_all([Size(code=size,name=dict(sizeChoices).get(size),user_id = current_user.id) for size in form.size.data])
db.session.add_all([Major(code=major,name=dict(majorChoices).get(major),user_id = current_user.id) for major in form.major.data])
db.session.add_all([Setting(code=setting,name=dict(settingChoices).get(setting),user_id = current_user.id) for setting in form.setting.data])
db.session.add_all([Region(code=reg,name=dict(regionChoices).get(reg),user_id = current_user.id) for reg in form.region.data])
db.session.add_all([State(code=state,name=dict(stateChoices).get(state),user_id = current_user.id) for state in form.state.data])
db.session.add_all([SpecPref(code=spec,name=dict(specPrefChoices).get(spec),user_id = current_user.id) for spec in form.specPref.data])

#update the user database with new data
updatedUser = User(id=current_user.id, relImp = form.relImp.data, sizeImp = form.sizeImp.data, allMajors = form.allMajors.data,
satMath = form.satMath.data, satEng = form.satEng.data, actMath = form.actMath.data, actEng = form.actEng.data, settingImp = form.settingImp.data,
regionImp = form.regionImp.data, stateImp = form.stateImp.data, income = form.income.data)
db.session.merge(updatedUser)

#TODO: reincrement after deleting?
#db.session.execute("DBCC CHECKIDENT (religions, RESEED, 0)") #doesn't work, does each user separately

db.session.commit()

Essentially, I delete all the current data in the tables and reinsert the new data into each table manually. This seems to work, but it messes up the IDs in the relationship tables (since they auto-increment, deleting entries does not reset the IDs). For my purposes, though, it is only an aesthetic problem, so this is the best way I could come up with, though it's not pretty.



Related Topics



Leave a reply



Submit