Skip to content
Advertisement

How to Update in sqlalchemy using objects?

I have a student table in postgres with the following columns Studentid | Student_name | Student_division

I am using sql alchemy and have modeled it as

     Class Student:
     _tablename__ : 'student'
     __table_args__ : {'schema' : 'student'}

     studentid=Column('Student_name', pk=true)
.
. And so on for rest of the columns.

#After creating object of the same
    stu = Student()

#I want the values be updated by user.
#The statement i have written is as
    x= session.query(Student).get(id) #id #given by user
    x.studentid= studentid #given by uset #models
    x.studentname = name
    session.add(x)
    session.commit()

How to update? This doesnt work.

Advertisement

Answer

you need to commit the changes and remove the session.add(x)

just keep session.commit()

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement