0

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'

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.