-1

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)
user3079103
  • 49
  • 1
  • 7
  • i believe you have to have an `.update()` call in there somewhere. see this answer: https://stackoverflow.com/a/278606/42346 – mechanical_meat Apr 03 '22 at 01:49
  • 1
    I tried that before and it wasn't working and couldn't resolve the error that came up but I just tried it again and it WORKED: `session.query(Conversation).filter_by(conversation_id=conversation_id).update({'answer_sent': True})...`Thank you for making me try again ;) – user3079103 Apr 03 '22 at 01:56
  • no problem! glad you got it working :) you can post an answer with the code that worked, it might help someone else. – mechanical_meat Apr 03 '22 at 02:00

1 Answers1

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()
user3079103
  • 49
  • 1
  • 7
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 03 '22 at 02:19