私はこれらのテーブルを持っています: 1) ユーザー
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key = True)
fname = Column(String(45))
lname = Column(String(45))
mail = Column(String(45))
password = Column(String(45))
courses = relationship("Course", secondary=user_course, backref="users")
universities = relationship("University", secondary=user_university, backref="users")
def add_university(self, university):
universities = self.universities
universities.append(university)
self.universities = universities
def add_course(self, course):
courses = self.courses
courses.append(course)
self.courses = courses
2) 大学
class University(Base):
__tablename__ = 'universities'
id = Column(Integer, primary_key=True)
date = Column(DateTime)
name = Column(String(45))
3) コース
class Course(Event):
__tablename__ = 'courses'
id = Column(Integer, ForeignKey('events.id'), primary_key=True)
name = Column(String(45))
year = Column(Integer)
__mapper_args__ = {
'polymorphic_identity':'course',
}
events = relationship("Event", secondary=course_event, backref="courses")
そのため、ユーザーコースとユーザー大学では多対多、コースイベント (もちろん親) では多対多です。
ユーザーでメソッドを定義して大学を追加しようとすると機能しますが、コースを追加しようとすると次のエラーが発生します。
トレースバック (最新の呼び出しが最後):
File "/Users/arnoutaertgeerts/Documents/Eclips/SlideTalk 2.0/test_SQL.py", line 473, in <module>
arnout.add_course(Analyse)
File "/Users/arnoutaertgeerts/Documents/Eclips/SlideTalk 2.0/test_SQL.py", line 101, in add_course
courses.append(course)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SQLAlchemy-0.7.9-py2.7.egg/sqlalchemy/orm/collections.py", line 1046, in append
item = __set(self, item, _sa_initiator)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SQLAlchemy-0.7.9-py2.7.egg/sqlalchemy/orm/collections.py", line 1021, in __set
item = getattr(executor, 'fire_append_event')(item, _sa_initiator)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SQLAlchemy-0.7.9-py2.7.egg/sqlalchemy/orm/collections.py", line 714, in fire_append_event
item, initiator)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SQLAlchemy-0.7.9-py2.7.egg/sqlalchemy/orm/attributes.py", line 800, in fire_append_event
value = fn(state, value, initiator or self)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SQLAlchemy-0.7.9-py2.7.egg/sqlalchemy/orm/attributes.py", line 1026, in emit_backref_from_collection_append_event
passive=PASSIVE_NO_FETCH)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SQLAlchemy-0.7.9-py2.7.egg/sqlalchemy/orm/attributes.py", line 854, in append
collection.append_with_event(value, initiator)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SQLAlchemy-0.7.9-py2.7.egg/sqlalchemy/orm/collections.py", line 655, in append_with_event
getattr(self._data(), '_sa_appender')(item, _sa_initiator=initiator)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SQLAlchemy-0.7.9-py2.7.egg/sqlalchemy/orm/collections.py", line 1046, in append
item = __set(self, item, _sa_initiator)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SQLAlchemy-0.7.9-py2.7.egg/sqlalchemy/orm/collections.py", line 1021, in __set
item = getattr(executor, 'fire_append_event')(item, _sa_initiator)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SQLAlchemy-0.7.9-py2.7.egg/sqlalchemy/orm/collections.py", line 714, in fire_append_event
item, initiator)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SQLAlchemy-0.7.9-py2.7.egg/sqlalchemy/orm/attributes.py", line 800, in fire_append_event
value = fn(state, value, initiator or self)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SQLAlchemy-0.7.9-py2.7.egg/sqlalchemy/orm/attributes.py", line 1020, in emit_backref_from_collection_append_event
_acceptable_key_err(state, initiator)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/SQLAlchemy-0.7.9-py2.7.egg/sqlalchemy/orm/attributes.py", line 982, in _acceptable_key_err
manager_of_class(initiator.class_)[initiator.key]))
ValueError: User.courses タイプの属性に関連付けられていないオブジェクト
ただし、イベントからコースを継承させるのをやめると、うまくいきます!
何か案は?