0

I am using SqlAlchemy with Flask and Postgresql. I have a basic insert operation but I would like the behavior of replace/upsert or on duplicate key update. Any help would be appreciated.

@auth_blueprint.route('/profile', methods=('GET', 'POST'))
def profile():
    try:
        session.pop('_flashes', None)
    except:
        pass
    user = Profile.query.filter_by(id=current_user.id).first_or_404()
    form = ProfileForm(obj=user)    
    if form.validate_on_submit():  
        form.populate_obj(profile)      
        ins = Profile(   
            id = current_user.id,     
            company = form.company.data,
            address1 = form.address1.data,
            address2 = form.address2.data,
            city = form.city.data,
            state = form.state.data,
            postal_code = form.postal_code.data,
            phone = form.phone.data
        )        
        db.session.add(ins)
        db.session.commit()   
        return redirect(url_for('admin.dashboard')) #submit data
    return render_template('auth/profile.html', form=form)
Mike C.
  • 1,761
  • 2
  • 22
  • 46
  • You probably want [this answer](https://stackoverflow.com/a/44395983/5320906) in the linked duplicate Q&A. – snakecharmerb Sep 25 '22 at 06:33
  • Is the `Profile` object that you want to update the same one that is (potentially) retrieved by `user = Profile.query.filter_by(id=current_user.id).first_or_404()` ? – Gord Thompson Sep 25 '22 at 23:39

0 Answers0