多对多订购

时间:2014-07-03 17:17:45

标签: python-2.7 sqlalchemy flask-sqlalchemy

我试图跟踪多对多关系的排序。我是否需要覆盖agenda.modules.append()方法?我可以根据agenda_id自动增加module_position字段吗?

from sqlalchemy.orm.collections import column_mapped_collection

agenda_modules = db.Table('agenda_modules',
    db.Column('module_id', db.Integer, db.ForeignKey('modules.id')),
    db.Column('agenda_id', db.Integer, db.ForeignKey('agendas.id')),
    db.Column('module_position', db.Integer) # Can I automatically increment this based on agenda_id? So it restarts at 1 per agenda_id?
)

class Agenda(db.Model):
    __tablename__ = 'agendas'

    modules = db.relationship('Module', secondary=agenda_modules, backref=db.backref('agendas', lazy='dynamic'),
                              collection_class=column_mapped_collection(agenda_modules.c.module_position))

>>> a.modules
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/siecje/Desktop/wr-ccafe/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 233, in __get__
    return self.impl.get(instance_state(instance), dict_)
  File "/home/siecje/Desktop/wr-ccafe/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 595, in get
    return self.set_committed_value(state, dict_, value)
  File "/home/siecje/Desktop/wr-ccafe/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 1023, in set_committed_value
    collection.append_multiple_without_event(value)
  File "/home/siecje/Desktop/wr-ccafe/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/collections.py", line 661, in append_multiple_without_event
    appender(item, _sa_initiator=False)
  File "/home/siecje/Desktop/wr-ccafe/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/collections.py", line 1499, in set
    key = self.keyfunc(value)
  File "/home/siecje/Desktop/wr-ccafe/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/collections.py", line 146, in __call__
    for col in self._cols(m)
  File "/home/siecje/Desktop/wr-ccafe/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 2301, in _get_state_attr_by_column
    prop = self._columntoproperty[column]
  File "/home/siecje/Desktop/wr-ccafe/venv/local/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 2709, in __missing__
    (column, self.mapper))
UnmappedColumnError: No column agenda_modules.module_position is configured on mapper Mapper|Module|modules...

0 个答案:

没有答案