Rapolas K.
Rapolas K.

Reputation: 1709

pyramid transaction.manager doesn't update object

How can I update a row in DB using transaction.manager in Pyramid? Here is what I have:

DBSession:

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))

View:

def create_update(request, post):
  title = request.POST.get('title', None)
  content = request.POST.get('content', None)

  post.title = title
  post.content = content

  with transaction.manager:
    if post.id is None:
        DBSession.add(post)
    transaction.commit()

This is how I get an existing post from DB:

def by_slug(slug):
  return DBSession.query(BlogPost).filter(BlogPost.slug == slug).first()

where BlogPost is a sqlalchemy model.

When I create a new post, everything is fine, it is added and saved in DB, however, nothing happens when I edit an existing post. I've tried DBSession.flush(), result is the same - I can create a new post, but existing one is not updated. What am I missing?

Upvotes: 0

Views: 648

Answers (2)

Rapolas K.
Rapolas K.

Reputation: 1709

Apparently, the issue was, that I didn't have pyramid_tm under pyramid.includes in ini configuration:

[app:main]
use = egg:myproject

pyramid.includes =
    pyramid_jinja2
    pyramid_tm #this was missing

Weird, that I was not seeing any errors or anything, and it sort of worked, but was giving a lot of headaches.

Upvotes: 0

Paul Yin
Paul Yin

Reputation: 1759

Why your use transaction here?
The ZopeTransactionExtension on the DBSession in conjunction with the pyramid_tm being active on your project will handle all commits for you.

so just try this:

def create_update(request, post):
  title = request.POST.get('title', None)
  content = request.POST.get('content', None)

  post.title = title
  post.content = content

  if post.id is None:
      DBSession.add(post)

Upvotes: 1

Related Questions