'SQLAlchemy ORM is not updating row

I am using SQLAlchemy ORM with MariaDB (mariadbconnector). The database updates with the add_conversation function but when trying to update a value it does nothing and there is no error. Instead of filter_by() I tried get() but then I received this error:

AttributeError: 'NoneType' object has no attribute 'answer_sent'

The row and column exist so I'm not sure where I am going wrong.

def add_conversation(conversation_id, chat_start, chat_end, answer_sent, answer_accepted):
    new_conversation = Conversation(conversation_id=conversation_id, chat_start=chat_start, chat_end=chat_end,              
    answer_sent=answer_sent, answer_accepted=answer_accepted)
    session.add(new_conversation)
    session.commit()

add_conversation(19156050, 1645552829, 1645559831, False, False)

def set_answer_sent(conversation_id):
    convo = session.query(Conversation).filter_by(conversation_id=conversation_id)
    convo.answer_sent = True
    session.commit()
    session.flush()

set_answer_sent(19156050)


Solution 1:[1]

Thank you @mechanical_meat

I just added .update({'answer_sent': True}) to the end of session.query and removed the convo variable assignments as they are not needed anymore:

def answer_was_sent(conversation_id):
    session.query(Conversation).filter_by(conversation_id=conversation_id).update({'answer_sent': True})
    session.commit()
    session.flush()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1