sqlachemy 获取新创建对象的id,flush与commit

2018-05-28 17:50:20 浏览数 (2)

代码语言:javascript复制
                for account_info in valid_account_detail:
                    try:
                        account = account_info.get('account')
                        password = account_info.get('password')
                        # date = account_info.get('date')
                        q1 = account_info.get('q1')
                        a1 = account_info.get('a1')
                        q2 = account_info.get('q2')
                        a2 = account_info.get('a2')
                        q3 = account_info.get('q3')
                        a3 = account_info.get('a3')

                        security1 = resource_apple_account.get_security_by_question_answer(q1, a1)
                        security2 = resource_apple_account.get_security_by_question_answer(q2, a2)
                        security3 = resource_apple_account.get_security_by_question_answer(q3, a3)
                        if not security1:  # 判断数据库中是否存在这个数据,没有则新建
                            security1 = resource_apple_account.AppleAccountSecurity(question=q1, answer=a1)
                            db.session.add(security1)
                            # 如果在这里写了commit,后面也可以取到这个新建数据的id,但是后面新建account对象时也要commit,
                            # 如果出现问题,rollback只会回滚最后一次commit,这里的就不能一起回滚了

                        if not security2:
                            security2 = resource_apple_account.AppleAccountSecurity(question=q2, answer=a2)
                            db.session.add(security2)

                        if not security3:
                            security3 = resource_apple_account.AppleAccountSecurity(question=q3, answer=a3)
                            db.session.add(security3)

                        # 新建对象
                        account_obj = resource_apple_account.AppleAccountCore(account=account, password=password,
                                                                              creator_id=creator.id)
                        db.session.add(account_obj)
                       
                        
                        
                        db.session.flush()
                        # 将数据刷新到数据库,这样才能取到新建对象的id,其实commit()也可以,但是为了保证rollback,
                        # 将整个事件回滚,所有用的flush,commit默认这次事件完成了,rollback只能回滚此次事件最后一次commit的内容,
                        # 如果在上面还有其他commit,则上面commit的内容不会被回滚,可以任务flush只是把数据先存入数据库,
                        # 但是此次事件还有彻底结束,但是commit就是彻底结束了

                        map1 = resource_apple_account.AppleAccountSecurityMap(apple_account_core_id=account_obj.id,
                                                                              apple_account_security_id=security1.id)
                        map2 = resource_apple_account.AppleAccountSecurityMap(apple_account_core_id=account_obj.id,
                                                                              apple_account_security_id=security2.id)
                        map3 = resource_apple_account.AppleAccountSecurityMap(apple_account_core_id=account_obj.id,
                                                                              apple_account_security_id=security3.id)
                        db.session.add_all([map1, map2, map3])
                        db.session.commit()

                    except Exception as e:
                        print u'错误信息', e
                        db.session.rollback()
                        return rest_base.restful_json(data=u'添加失败', code=406)

                return rest_base.restful_json(data=u'添加成功')

0 人点赞