请注意,这不是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), )
当我尝试通过以下方式创建时:
@classmethod
def create(cls, user, articles):
return TrustmileDelivery(user, articles)
的初始(..)函数为:
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'
最佳答案
正如@van所说的,这只是脑子里的事,答案是使用self.articles.extend(articles)
而不是self.articles.append(articles)
。
关于python - 附加到SQLAlchemy列表时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34654646/