简体   繁体   English

SQLAlchemy - ObjectDeletedError: 实例 '<class at...> ' 已被删除。 帮助</class>

[英]SQLAlchemy - ObjectDeletedError: Instance '<Class at...>' has been deleted. Help

I'm having some issues with deleting rows from a database and then adding new ones.我在从数据库中删除行然后添加新行时遇到了一些问题。 Here's the code:这是代码:

for positionid in form_result['responsibilities']:
   inputdata = form_result['responsibilities'][positionid]

    self.__deleterow(dbmyaccount.Responsibilities, session['authed']['userid'])

    for resp in (i.strip() for i in inputdata.split(',')):
        resp_q = dbmyaccount.Responsibilities(session['authed']['userid'])

        resp_q.positionid     = positionid
        resp_q.responsibility = resp

        Session.add(resp_q)
        Session.commit()

def __deleterow(self, table, user):       
    delete_q = Session.query(table).filter_by(userid=user).first()

    if delete_q:
        Session.query(table).filter_by(userid=user).delete()
        Session.commit()

Basically, I wipe all the users data from the table then add in their new options.基本上,我从表中擦除所有用户数据,然后添加他们的新选项。 The problem is, the code produces this error:问题是,代码产生了这个错误:

ObjectDeletedError: Instance '<Responsibilities at ...>' has been deleted.

I have no idea why... From what my Google searches have turned up, the error is produced because I'm modifying the Responsibilities class after having deleted all it's data from the database.我不知道为什么......从我的谷歌搜索结果来看,产生错误是因为我在从数据库中删除所有数据后修改了 Responsibilities 类。 I can't figure out how to 'let go' of the class though to reinitialise it with the new data.尽管用新数据重新初始化它,但我不知道如何“放手”这个类。

What am I doing wrong?我究竟做错了什么?

EDIT编辑

Here's the Responsibility class:这是责任类:

class Responsibilities(Base):

__tablename__ = 'responsibilities'

id             = Column(Integer, primary_key=True)
userid         = Column(Integer, ForeignKey('users.id'))
positionid     = Column(Integer)
responsibility = Column(String(50))

def __init__(self, user=None):
    if user:
        self.userid = user

def __repr__(self):
    return "<Responsibilities({0})".format(self.userid)

And here's the traceback:这是追溯:

File '<string>', line 2 in save
File 'C:\\Users\\Dave\\Documents\\Python\\pylons\\mydevenv\\lib\\site-packages\\pylons-1.0-py2.6.egg\\pylons\\decorators\\rest.py', line 33 in check_methods
  return func(*args, **kwargs)
File 'C:\\Users\\Dave\\Documents\\Python\\pylons\\website\\website\\controllers\\myaccount.py', line 260 in save
  self.__deleterow(dbmyaccount.Responsibilities, session['authed']['userid'])
File 'C:\\Users\\Dave\\Documents\\Python\\pylons\\website\\website\\controllers\\myaccount.py', line 210 in __deleterow
  Session.query(table).filter_by(userid=user).delete()
File 'C:\\Users\\Dave\\Documents\\Python\\pylons\\mydevenv\\lib\\site-packages\\sqlalchemy-0.6.3-py2.6.egg\\sqlalchemy\\orm\\query.py', line 2031 in delete
  eval_condition(obj)]
File 'C:\\Users\\Dave\\Documents\\Python\\pylons\\mydevenv\\lib\\site-packages\\sqlalchemy-0.6.3-py2.6.egg\\sqlalchemy\\orm\\evaluator.py', line 82 in evaluate
  left_val = eval_left(obj)
File 'C:\\Users\\Dave\\Documents\\Python\\pylons\\mydevenv\\lib\\site-packages\\sqlalchemy-0.6.3-py2.6.egg\\sqlalchemy\\orm\\evaluator.py', line 42 in <lambda>
  return lambda obj: get_corresponding_attr(obj)
File 'C:\\Users\\Dave\\Documents\\Python\\pylons\\mydevenv\\lib\\site-packages\\sqlalchemy-0.6.3-py2.6.egg\\sqlalchemy\\orm\\attributes.py', line 163 in __get__
  instance_dict(instance))
File 'C:\\Users\\Dave\\Documents\\Python\\pylons\\mydevenv\\lib\\site-packages\\sqlalchemy-0.6.3-py2.6.egg\\sqlalchemy\\orm\\attributes.py', line 382 in get
  value = callable_(passive=passive)
File 'C:\\Users\\Dave\\Documents\\Python\\pylons\\mydevenv\\lib\\site-packages\\sqlalchemy-0.6.3-py2.6.egg\\sqlalchemy\\orm\\state.py', line 280 in __call__
  self.manager.deferred_scalar_loader(self, toload)
File 'C:\\Users\\Dave\\Documents\\Python\\pylons\\mydevenv\\lib\\site-packages\\sqlalchemy-0.6.3-py2.6.egg\\sqlalchemy\\orm\\mapper.py', line 2441 in _load_scalar_attributes
  state_str(state))
ObjectDeletedError: Instance '<Responsibilities at ...>' has been deleted.

When you do a bulk delete, it deletes the rows from the databases but does not update references to the in-memory objects in the SQLAlchemy session.当您执行批量删除时,它会从数据库中删除行,但不会更新对 SQLAlchemy 会话中内存中对象的引用。 You can use the synchronize_session argument to .delete() to get it to sync the session:您可以使用.delete()synchronize_session参数来让它同步会话:

Session.query(table).filter_by(userid=user).delete(synchronize_session='fetch')

See the warning in the docs: http://docs.sqlalchemy.org/en/rel_1_0/orm/query.html?highlight=query.delete#sqlalchemy.orm.query.Query.delete请参阅文档中的警告: http ://docs.sqlalchemy.org/en/rel_1_0/orm/query.html?highlight=query.delete#sqlalchemy.orm.query.Query.delete

The code you're trying to execute is valid and I guess the fault lies at your mapper configuration, specifically, your User-Responsibilities relationship.您尝试执行的代码是有效的,我想问题出在您的映射器配置上,特别是您的用户职责关系。 What you should do is to set the proper cascade property for this relationship, you should be able to figure out the proper setting for your db schema, by following the guidelines given here [the cascade item]: http://www.sqlalchemy.org/docs/orm/relationships.html#the-relationship-api您应该做的是为这种关系设置正确的cascade属性,您应该能够按照此处给出的指南[级联项]: http://www.sqlalchemy 为您的数据库模式找出正确的设置。 org/docs/orm/relationships.html#the-relationship-api

I had similar problem on creating tests.我在创建测试时遇到了类似的问题。 I had relationship configured with "cascade" but the issue was related to the fact on creating responsibilities, user.responsibilities need to be filled as well.我已将关系配置为“级联”,但问题与创建职责有关,用户也需要填写职责。

Here's to the next person!这是给下一个人的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 记录不存在或已被删除。\\n(record: account.move.line(5398,), user: 7) in odoo 同时更新 - record does not exist or has been deleted.\\n(record: account.move.line(5398,), user: 7) in odoo while updating 实例<xxx>在 commit() 之后已被删除,但何时以及为什么? - Instance <xxx> has been deleted after commit(), but when and why? “SQLAlchemy”的实例没有“...”成员 - Instance of 'SQLAlchemy' has no '...' member 为什么即使生成它的实例已被删除,记录器仍然存在? - Why does logger persist even when instance spawning it has been deleted? “SQLAlchemy”的实例没有“列”成员 - Instance of 'SQLAlchemy' has no 'Column' member Python class 如果原始 python 文件已被删除,真的不可能解开吗? - Is it really impossible to unpickle a Python class if the original python file has been deleted? 用户已被删除/停用(由 GetDialogsRequest 引起) - The user has been deleted/deactivated (caused by GetDialogsRequest) 告知实例是否已销毁 - Telling if an Instance has been Destroyed 皮林特误报“<class 'runtimeerror'> :包装的 C/C++ object 类型的 QApplication 已被删除”</class> - Pylint false positive “<class 'RuntimeError'>: wrapped C/C++ object of type QApplication has been deleted” 从数据库中删除的图像,从存储中删除的图像,太 - Images that has been deleted from database,be deleted from storage,too
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM