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

时间:2010-08-14 04:14:32

标签: python sql orm sqlalchemy pylons

我在从数据库中删除行然后添加新行时遇到一些问题。这是代码:

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()

基本上,我从表中擦除所有用户数据,然后添加新选项。问题是,代码产生了这个错误:

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

我不知道为什么......从我的Google搜索结果出现后,出现了错误,因为我在从数据库中删除了所有数据后修改了Responsibilities类。虽然用新数据重新初始化,但我无法弄清楚如何“放手”。

我做错了什么?

修改

这是责任类:

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)

这是追溯:

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.

3 个答案:

答案 0 :(得分:5)

执行批量删除时,它会从数据库中删除行,但不会更新SQLAlchemy会话中对内存中对象的引用。 您可以使用synchronize_session参数.delete()来使其同步会话:

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

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

答案 1 :(得分:2)

您尝试执行的代码是有效的,我猜错误在于您的映射器配置,特别是您的用户 - 责任关系。你应该做的是为这种关系设置正确的cascade属性,你应该能够通过遵循这里给出的指导[ cascade 来找出你的数据库模式的正确设置。项目]: http://www.sqlalchemy.org/docs/orm/relationships.html#the-relationship-api

答案 2 :(得分:0)

我在创建测试时遇到类似的问题。我已将关系配置为“级联”,但问题与创建责任这一事实有关,用户责任也需要填写。

这是下一个人!