anils
anils

Reputation: 1972

How to update table row with SQLAlchemy?

I want to update a row of a table.

To add I used,

session.add(unit) #here unit is unit object of class unit
session.commit()

To update I tried,

session.merge(unit)
session.commit()

However, in the database there are other columns, e.g. create_by, created_on, updated_on, which are not set in the unit object, so these are set NULL upon the merge.

Upvotes: 2

Views: 1426

Answers (2)

Pakman
Pakman

Reputation: 2210

You can just modify properties of the class you use to represent the row and call session.commit(). For example:

# Add a new user
user = User(name="John", age=20)
session.add(user)
session.commit()

# Modify the user we just added
user.age = 21
session.commit()

Upvotes: 4

anils
anils

Reputation: 1972

I used merge(unit), by modifying model structure. Initialization of create_by, created_on, updated_on etc. removed from constructor init(...)

Upvotes: -1

Related Questions