sqlalchemy.exc.InvalidRequestError:无法附加实例另一个具有密钥的实例已存在于此会话中

时间:2018-02-03 19:23:00

标签: python python-3.x sqlalchemy

我有一个错误,我无法找到解决方案。请原谅我的代码质量,直到6个月前我才开始上课。我试图用expunge分离类别对象但是一旦它被添加它就无法工作。我在想当用expunge分离对象时它会起作用。我找不到解决方案:(。我尽可能多地粘贴代码,所以你可以看到

    Traceback (most recent call last):
  File "/home/scwall/PycharmProjects/purebeurre/recovery.py", line 171, in <module>
    connection.connect.add(article)
  File "/home/scwall/PycharmProjects/purebeurre/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1776, in add
    self._save_or_update_state(state)
  File "/home/scwall/PycharmProjects/purebeurre/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1796, in _save_or_update_state
    self._save_or_update_impl(st_)
  File "/home/scwall/PycharmProjects/purebeurre/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2101, in _save_or_update_impl
    self._update_impl(state)
  File "/home/scwall/PycharmProjects/purebeurre/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2090, in _update_impl
    self.identity_map.add(state)
  File "/home/scwall/PycharmProjects/purebeurre/venv/lib/python3.6/site-packages/sqlalchemy/orm/identity.py", line 149, in add
    orm_util.state_str(state), state.key))
sqlalchemy.exc.InvalidRequestError: Can't attach instance <Categories at 0x7fe8d8000e48>; another instance with key (<class 'packages.databases.models.Categories'>, (26,), None) is already present in this session.

Process finished with exit code 1







class CategoriesQuery(ConnectionQuery):

@classmethod
def get_categories_by_tags(cls, tags_list):
    return cls.connection.connect.query(Categories).filter(Categories.id_category.in_(tags_list)).all()

其他档案:

 def function_recovery_and_push(link_page):
        count_and_end_page_return_all = {}
        count_f = 0
        total_count_f = 0
        list_article = []
        try:
            products_dic = requests.get(link_page).json()
            if products_dic['count']:
                count_f = products_dic['page_size']
            if products_dic['count']:
                total_count_f = products_dic['count']
            if not products_dic['products']:
                count_and_end_page_return_all['count'] = False
                count_and_end_page_return_all['total_count'] = False
                count_and_end_page_return_all['final_page'] = True
            else:
                count_and_end_page_return_all['final_page'] = False
            for product in products_dic["products"]:
                if 'nutrition_grades' in product.keys() \
                        and 'product_name_fr' in product.keys() \
                        and 'categories_tags' in product.keys() \
                        and 1 <= len(product['product_name_fr']) <= 100:
                    try:
                        list_article.append(
                            Products(name=product['product_name_fr'], description=product['ingredients_text_fr'],
                                     nutrition_grade=product['nutrition_grades'], shop=product['stores'],
                                     link_http=product['url'],
                                     categories=CategoriesQuery.get_categories_by_tags(product['categories_tags'])))

                    except KeyError:
                        continue

            count_and_end_page_return_all['count'] = count_f
            count_and_end_page_return_all['total_count'] = total_count_f
            list_article.append(count_and_end_page_return_all)
            return list_article

        except:
            count_and_end_page_return_all['count'] = False
            count_and_end_page_return_all['total_count'] = False
            count_and_end_page_return_all['final_page'] = True
            list_article.append(count_and_end_page_return_all)
            return list_article


    p = Pool()
    articles_list_all_pool = p.map(function_recovery_and_push, list_page_for_pool)
    p.close()

    for articles_list_pool in articles_list_all_pool:
        for article in articles_list_pool:
            if type(article) is dict:
                if article['count'] != False and article['total_count'] != False:
                    count += article['count']
                    total_count = article['total_count']

                if article['final_page'] is True:
                    final_page = article['final_page']

            else:
                connection.connect.add(article)

我收到此错误信息,请提前感谢您的回答

2 个答案:

答案 0 :(得分:0)

当您尝试将对象添加到会话但已经加载时会发生此错误。

我看到你使用的唯一一行.add函数就在你运行的最后: connection.connect.add(article)

所以我的猜测是这个模型已经加载到会话中,你不需要再次添加它。您可以添加一个try,但是如果它抛出异常,则回滚该操作。

答案 1 :(得分:0)

有同样的问题,不确定您是否以与我相同的方式实现了模型,但至少在我的情况下,我在表的模型中拥有-即:

function email() 
{

  //setup function
  var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Issues");  
  var StartRow = 2; //first row of data to process
  var LastRow = ActiveSheet.getLastRow();
  var RowRange = LastRow - StartRow + 1;
  var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,35);
  var AllValues = WholeRange.getValues();
  var message = "";    
  for (var i=0;i<AllValues.length;i++) 
   
    {
      var CurrentRow = AllValues[i];
      var EmailSent = CurrentRow[34]; 
      if (CurrentRow[33] == "Ready to Email" && EmailSent!= "sent") 
      {
        message +=                      //set HTML template for information
          "<p><b>Issue Status: </b>" + CurrentRow[0] + "</p>" +
          "<p><b>Group: </b>" + CurrentRow[1] + "</p>" +
          "<p><b>LifeCycle Status: </b>" + CurrentRow[2] + "</p>" +  
          "<p><b>Issue Number: </b>" + CurrentRow[3] + "</p>" +
          "<p><b>Priority: </b>" + CurrentRow[4] + "</p>" +
          "<p><b>Opened By: </b>" + CurrentRow[10] + "</p>"; 
        var setRow = i + StartRow;
        var SendTo = "myemail@test.com";
        var Subject = "New Issue reported:  " + CurrentRow[3];    //set subject line   
        //send the actual email  
           MailApp.sendEmail
                  ({
                        to: SendTo,
                        cc: "",
                        subject: Subject,
                        htmlBody: message,
                  });
  
         ActiveSheet.getRange(setRow, 35).setValue("sent");  //update the row if email is sent
    }  
  }
}

所以后来我尝试做

product_items = relationship(...)

它引发与您相同的异常:

products = session.query(Products).all()

one_of_the_products = products[0]
new_product = ProductItem(product_id=one_of_the_products.id, name='foo', category='bla')
session.add(new_product)

发生异常的原因是,当我查询sqlalchemy.exc.InvalidRequestError: Can't attach instance <ProductItem at 0x7fe8d8000e48>; another instance with key (<class 'packages.databases.models.ProductItem'>, (26,), None) is already present in this session. -products创建了它自己的子查询并附加了relationship的对象时,它将它们放置在我在product_item-> relationship()中定义的变量名称。

所以不要这样做:

product_items

我只需要使用这种关系:

session_add(new_product)

希望它对其他人有帮助。

相关问题