sqlachemy Get the id of the newly created object, flush and commit

                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: # Determine whether this data exists in the database, if not, create a new one
                            security1 = resource_apple_account.AppleAccountSecurity(question=q1, answer=a1)
                            db.session.add(security1)
                            # If you write commit here, you can also get the id of the new data later, but you also need to commit when you create an account object later.
                            # If there is a problem, rollback will only roll back the last commit, and the ones here cannot be rolled back together

                        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)

                        # new object
                        account_obj = resource_apple_account.AppleAccountCore(account=account, password=password,
                                                                              creator_id=creator.id)
                        db.session.add(account_obj)
                       
                        
                        
                        db.session.flush()
                        # Refresh the data to the database, so that the id of the newly created object can be obtained. In fact, commit() can also be used, but in order to ensure rollback,
                        # Roll back the entire event, all flush and commit used are completed by default, and rollback can only roll back the content of the last commit of this event.
                        # If there are other commits on it, the content of the above commits will not be rolled back, and the task flush just stores the data in the database first.
                        # But this incident is still completely over, but commit is completely over

                        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'error message', e
                        db.session.rollback()
                        return rest_base.restful_json(data=u'Add failed', code=406)

                return rest_base.restful_json(data=u'Added successfully')

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324980823&siteId=291194637
Recommended