SqlAlchemy update not working with Sqlite

前端 未结 1 1649
走了就别回头了
走了就别回头了 2021-02-14 02:41

I followed the (two) examples in this question: SQLAlchemy: a better way for update with declarative?

And I\'m finding that a model update does not happen when using sql

1条回答
  •  北荒
    北荒 (楼主)
    2021-02-14 03:17

    So I tried several different things. Here's what didn't work, and what finally did work:

    Didn't work:

    # this will throw an exception, "AttributeError: 'Task' object has no attribute 'update'"
    db.session.query(Task).get(id).update({"state":state})
    db.session.commit()
    
    # this was the example in the linked SO thread:
    # does nothing
    db.session.query(Task).filter_by(id=id).update({"state":state})
    
    #this also does nothing
    task = Task.query.filter_by(id=id)
    task.state = state
    db.session.commit()
    
    #not this either
    task = Task.query.get(id)
    task.state = state
    db.session.commit()
    

    Did work:

    #ok this works:
    db.session.query(Task).filter_by(id=id).update({"state":state})
    db.session.commit()
    
    #and also this:
    task = db.session.query(Task).get(id)
    task.state = state
    db.session.commit()
    

    0 讨论(0)
提交回复
热议问题