我有这些表格: 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类型的属性无关的对象
但是,如果我停止让课程从Event继承,它确实有效!
有什么想法吗?