... because from time to time I'm a web developer, too
About me
Projects
Contact
Links

SQLAlchemy and update data using ORM

August 17, 2009 | databases, python
author: Karol Zielinski | comments: 0 | views: 960
Tags: , ,

I was looking for some method to update data using SQLAlchemy‘s ORM. And? I didn’t find any usefull informations in SQLAlchemy’s documentation. That’s why I started to looking for some informations using google and…

  1. In SQLAlchemy there is a method update(), but… update(), save_or_update(), save() are all deprecated(!)
  2. Everything I need is to use add() method

So… in quick example:

if I want to add data to my database…


cm = Some_Object()
cm.column1 = column1_data
cm.column2 = column2_data
cm.column3 = column3_data

Session.add(cm)
Session.commit()

and when I want to update data…


cs = Session.query(Some_Object).filter(
and_(
Some_Object.id == some_id,
Some_Object.other_id == some_other_id
)
).one()

cs.column1 = "abc"
cs.column2 = "qwe"
cs.column3 = "asd"

Session.add(cs)
Session.commit()

So… SQLAlchemy “knows” what we want to do

Bookmark and Share
Post SQLAlchemy and update data using ORM to develway Post SQLAlchemy and update data using ORM to Delicious Post SQLAlchemy and update data using ORM to Digg Post SQLAlchemy and update data using ORM to Facebook Post SQLAlchemy and update data using ORM to Reddit Post SQLAlchemy and update data using ORM to StumbleUpon

Related news and resources

Write a comment

Karol Zielinski :: Just a tech stuff Hello, I'm Karol Zielinski, internet evangelist, an entrepreneur, project manager and a web developer from Gdynia, Poland. I like creative design, good advertisement, social media and all kind of stuff around the web.

Most popular posts

Much more links

Karol Zielinski    |   contact me
Gdynia, Poland
RSS - Just a tech stuff - python, java blog - web development blog Karol Zielinski on twitter Karol Zielinski on LinkedIn Karol Zielinski on facebook Karol Zielinski on delicious Karol Zielinski on digg Karol Zielinski on flickr Karol Zielinski on stumbleupon Karol Zielinski on technorati