蟒蛇。 SQL炼金术。对象未绑定到会话。属性刷新操作无法继续

时间:2019-04-30 01:00:30

标签: python sqlalchemy

当我的部分代码运行时,我会有一些回溯。

  

[“ 0x7f1101d1e3c8处的实例请求未绑定到会话;   属性刷新操作无法进行”]

当我尝试返回current_request.id时,实际上发生了跟踪。也许有人遇到过类似的问题。我将为您的决定提供帮助。

        current_request = db_session.query(
            Request
        ).filter(
            Request.id == request_id
        ).one()

        current_request.catalog_request_priority_id = catalog_request_priority.id
        current_request.contact_phone = contact_phone
        current_request.contact_email = contact_email
        current_request.service_id = service.id
        current_request.is_emergency = is_emergency
        current_request.is_paid = is_paid
        current_request.is_planned = is_planned
        db_session.commit()

        current_document = db_session.query(
            Document
        ).filter(
            Document.id == current_request.document_id
        ).one()

        if service.catalog_service_category_id == 2:
            current_document.catalog_document_type_id = 2
        if service.catalog_service_category_id == 3:
            current_document.catalog_document_type_id = 1

            current_document.description = description
            current_document.external_number = external_number
            current_document.modified_by = author_id
            current_document.modified_at = datetime.utcnow()
        db_session.commit()

        if service.catalog_service_category_id not in [2, 3]:
            raise MethodException(
                'Incorrect service category'
            )
        if current_request.is_emergency is True and current_request.sla_id is None:
            current_request.sla_id = 6
        if current_request.is_emergency is False and current_request.sla_id is None:
            current_request.sla_id = 4
            db_session.commit()

        current_request.planned_reaction_time = self.__get_planned_reaction_time(
            sla_id=current_request.sla_id,
            user_session_container=user_session_container
        )
        db_session.commit()
        return current_request.id

    @staticmethod
    def __get_planned_reaction_time(user_session_container, sla_id):
        db_session = user_session_container.db_session
        planned_reaction_time_for_this_sla = db_session.query(
            Sla
        ).filter(
            Sla.id == sla_id
        ).one()
        reaction_time = planned_reaction_time_for_this_sla.reaction_time
        planned_reaction_time_for_current_request = datetime.now() + reaction_time
        return planned_reaction_time_for_current_request

0 个答案:

没有答案