1

I have the following model:

class Employee (Base):
    __tablename__ = 'employees'

    id       = Column(Integer, primary_key=True, autoincrement=True)
    name     = Column(String(300), unique=True, nullable=False)
    phone_a  = Column(String(20), nullable=False)
    phone_b  = Column(String(20))
    email_a  = Column(String(400), nullable=False)
    email_b  = Column(String(400))
    address  = Column(String)
    charge   = Column(String(100), nullable=False)
    active   = Column(Boolean, default=True)

    created  = Column(DateTime, nullable=False, default=datetime.datetime.now)
    modified = Column(DateTime, onupdate=datetime.datetime.now)

    def __init__(self):
        self.active  = True
        self.created = datetime.datetime.now()

    def __unicode__(self):
        return self.name

I wrote the add view for it, very basic:

employee = Employee()
form     = Form(request, EmployeeSchema(), obj = employee)

if form.validate():
    employee = form.bind(Employee())
    try:
        DBSession.add(employee)
        DBSession.flush()
        return HTTPFound(location = request.route_url('employees'))
    except IntegrityError:
        message = 'Oops!'

And it works well. But the UPDATE view doesn't. I just does not save. According to the tutorial basically with the same code it should work. But it doesn't, SQLAlchemy tries to insert a new object instead of just updating it. I tried

import transaction
transaction.commit()

But no success.

_id = request.matchdict['employeeid']
employee = DBSession.query(Employee).filter_by(id=_id).first()
form     = Form(request, EmployeeSchema(), obj = employee)

if form.validate():
    employee    = form.bind(Employee())
    try:
        DBSession.add(employee)
        return HTTPFound(location = request.route_url('employees'))
    except IntegrityError:
        message = ''

1 Answer 1

4

You need to bind to the item, you do not need to add a new Employee() instance:

_id = request.matchdict['employeeid']
employee = DBSession.query(Employee).get(_id)
form     = Form(request, EmployeeSchema(), obj=employee)

if form.validate():
    form.bind(employee)
    return HTTPFound(location = request.route_url('employees'))

That's it.

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

8 Comments

Shouldn't you commit the session after the bind?
The session is automatically committed by the pyramid transaction middleware.
K. In that case, why do you need to add to the session? Isn't it already in the session using the query? Or is this another Pyramid thing?
It's been a while; replaced with a merge.
For everyone following this without success: Be sure to have pyramid_tm in your pyramid.includes (in your ini-file). Otherwise there will be no middleware to automatically commit. I started my project with a scaffold without DB, and it took me some time to find that problem.
|

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.