Sqlachemy get the Id,flush and commit of the newly created object

Source: Internet
Author: User
Tags flush rollback

                For account_info in Valid_account_detail:try:account = account _info.get (' account ') Password = account_info.get (' password ') # date = Accoun                        T_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 = Resour Ce_apple_account.get_security_by_question_answer (Q1, a1) Security2 = Resource_apple_account.get_sec Urity_by_question_answer (Q2, a2) Security3 = Resource_apple_account.get_security_by_question_answer (Q3, A3) if not security1: # Determine if this data exists in the database, no new security1 = Resource_ Apple_account. AppleaccouNtsecurity (question=q1, ANSWER=A1) Db.session.add (security1) # If you're here                         Write a commit, the following can also fetch the ID of the new data, but the next new account object to commit, # If there is a problem, rollback will only rollback the last commit, here can not be rolled back together If not security2:security2 = Resource_apple_account. Appleaccountsecurity (QUESTION=Q2, ANSWER=A2) db.session.add (security2) I F 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 () # Flushes the data to the database so that the ID of the newly created object can be taken, but the commit () is also possible, but to protect                        Rollback, # Roll the whole event back, all the Flush,commit by default this event is complete, rollback can only roll back the last commit of this event,                        # If there are other commits above, then the contents of the above commit will not be rolled back, but the task flush can just put the data into the database first, # But this event is completely over, but the 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 ' Add success ')

  

Sqlachemy get the Id,flush and commit of the newly created object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.