Home > Mobile >  SQLachemy flask - How to update values in database. getting: AttributeError: 'scoped_session�
SQLachemy flask - How to update values in database. getting: AttributeError: 'scoped_session�

Time:07-14

So I have a pandas dataframe (dfm) from which I have calculated new values from based on user input. I want to place these update/changed values into the database. I feel like I should just be able to session commit, and then I think I need to use update since the data already exists.

def report_maker(report_id):
   # other code ....
   if dfm['report'][r]==0:
        db.session.add(report_a)
        # the above works just fine. 
   else:
        report_b= db.session.query(Standard).filter(Standard.report_id==report_id, Standard.code == dfm['code'][r])
        report_b.code=dfm['code'][r]
        report_b.name=dfm['name'][r]
        report_b.standard=dfm['standard'][r] 
        report_b.level=dfm['level'][r] 
        report_b.report_id=report_id
        db.session.update(report_b)    #### the error is right here .!.!
   try:
        db.session.commit()
   except:
        print(report_a.name)

The error is

AttributeError: 'scoped_session' object has no attribute 'update'

CodePudding user response:

The update() method is an attribute of the Query Object not the Session Object. If you want to update multiple rows you can pass a dictionary representation of all columns that need to be updated:

db.session.query(Foo).filter(Foo.foo == foo).update(values={'bar': bar})

If you are filtering for a unique primary_key, you could also consider to update the Row Object

foo = db.session.query(Foo).filter(Foo.foo = foo).first()
foo.bar = bar
db.session.commit()

For further reading I would recommend SQLAlchemy documention.

  • Related