附加到SQLAlchemy列表时出错

时间:2016-01-07 12:01:50

标签: python sqlalchemy

请注意,这不是this issue

的副本

我只是尝试添加到我的模型中的one_to_many关系中定义的列表,如下所示。 这是我的模特:

class TrustmileDelivery(db.Model, UniqueMixin, TableColumnsBase):
    __tablename__ = 'trustmile_delivery'

    articles = one_to_many('Article', backref='delivery', lazy='select')
    # Problem with this is getting a cascade error
    neighbour = many_to_one('ConsumerUser', backref='trustmile_deliveries', lazy='select')
    courier_user = many_to_one('CourierUser', backref='deliveries', lazy='select')
    state = db.Column(db.String(255), )

当我尝试创建via:

 @classmethod
    def create(cls, user, articles):
        return TrustmileDelivery(user, articles)

使用 init (..)函数:

def __init__(self, courier_user, articles):
    self.courier_user = courier_user
    if len(articles):
        self.articles.append(articles)

我得到以下堆栈跟踪:

Traceback (most recent call last):
  File "/Users/james/Documents/workspace/trustmile-backend/trustmile/tests/test_deliveries.py", line 66, in test_create_trustmile_delivery
    tm_delivery = TrustmileDelivery.create(courier_user, articles)
  File "/Users/james/Documents/workspace/trustmile-backend/trustmile/app/deliveries/model.py", line 419, in create
    return TrustmileDelivery(user, articles)
  File "<string>", line 4, in __init__
  File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/orm/state.py", line 306, in _initialize_instance
    manager.dispatch.init_failure(self, args, kwargs)
  File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 60, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/orm/state.py", line 303, in _initialize_instance
    return manager.original_init(*mixed[1:], **kwargs)
  File "/Users/james/Documents/workspace/trustmile-backend/trustmile/app/deliveries/model.py", line 408, in __init__
    self.articles.append(articles)
  File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/orm/collections.py", line 1072, in append
    item = __set(self, item, _sa_initiator)
  File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/orm/collections.py", line 1044, in __set
    item = executor.fire_append_event(item, _sa_initiator)
  File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/orm/collections.py", line 716, in fire_append_event
    item, initiator)
  File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 936, in fire_append_event
    initiator or self._append_token or self._init_append_token())
  File "/Users/james/.virtualenvs/trustmile-api-p2710/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 42, in append
    item_state = attributes.instance_state(item)
AttributeError: 'list' object has no attribute '_sa_instance_state'

1 个答案:

答案 0 :(得分:4)

正如@van所说,只是一个大脑快照,答案是使用self.articles.extend(articles)代替self.articles.append(articles)