OperationalError :( OperationalError)没有这样的列

时间:2013-08-20 07:43:01

标签: python-2.7 sqlalchemy flask-sqlalchemy

我有问题。我必须这样查询:

@app.route('/api/subscriptions/<string:id>', methods=('DELETE',))
@decorators.login_required
def delete_subscription(id):
    dbsession = DBSession()
    session = Session()
    favorit = (dbsession.query(StudentsFavorites)
      .filter(Exams.number == str(id))
      .filter(StudentsFavorites.exam_id)
      .filter(Students.id == StudentsFavorites.student_id)
      .filter(Students.id == str(session.get_user_id()))
      .delete()         )
    dbsession.flush()
    return jsonify(error=False)

但是当我执行此查询时,我得到了这个例外:

OperationalError: (OperationalError) no such column: exams.number u'DELETE FROM students_favorites WHERE exams.number = ? AND students_favorites.exam_id AND students.id = students_favorites.student_id AND students.id = ?' ('123123123', 'a24213')

表非常大并且获得了大量信息,因此我无法发布所有信息。但是这个查询有效:

@app.route('/api/subscriptions/<string:id>', methods=('PUT',))
@decorators.login_required
def add_subscription(id):
    dbsession = DBSession()
    session = Session()
    examID = (dbsession.query(Exams.id)
     .filter(Exams.number == id).first()
     )
    favorit=StudentsFavorites(student_id=session.get_user_id(), exam_id=examID.id)
    dbsession.add(favorit)
    dbsession.flush()
    return jsonify(error=False)

表格的简短视图:

table: Exams
rows: id, number (number is the id i put into the function)

table: StudentsFavorites
rows: student_id, exams_id

table: Students
rows: id

我真的不明白,为什么他没有在异常中找到数字行。

修改

数据库学生收藏:

class StudentsFavorites(Base):
    """N:M resolve model for the exams to the semester.
    """

    __tablename__ = "students_favorites"

    student_id = Column(Unicode(255), ForeignKey("students.id"), primary_key=True, autoincrement=False)

    exam_id = Column(Integer, ForeignKey("exams.id"),            primary_key=True, autoincrement=False)

    created_at = Column(DateTime, nullable = False, default = datetime.now)

    student = relationship("Students", uselist = False, lazy="joined")

    exam = relationship("Exams", uselist=False, lazy="joined")

这样的东西?我试过这个:

    (dbsession.query(StudentsFavorites)
           .filter(StudentsFavorites.exam.id == str(id))
           .filter(StudentsFavorites.student.id == str(session.get_user_id()))
           .delete()
 )

但是得到了错误,那个id在考试/学生中不存在

1 个答案:

答案 0 :(得分:0)

你有两个同样问题的案例。您的查询包含StudentFavorites的信息,这意味着它了解StudentFavorites.student_idStudentFaovrites.exams_id。它对Students.idExames.idExames.number一无所知。 In order for you to query a StudentFavorites object and have it know about those other values you're going to have to perform a sql join

在sqlalchemy工作时,加入可能会有点麻烦(好吧......在常规的SQL中也是如此)。由于我不知道你的表模式是什么,我不能谈论它,但视图应该看起来像这样。

@app.route('/api/subscriptions/<string:id>', methods=('DELETE',))
@decorators.login_required
def delete_subscription(id):
    dbsession = DBSession()
    session = Session()
    favorit = (dbsession.query(StudentsFavorites)
      .join(Exames)
      .join(students)
      .filter(Exams.number == str(id))
      .filter(StudentsFavorites.exam_id)
      .filter(Students.id == StudentsFavorites.student_id)
      .filter(Students.id == str(session.get_user_id()))
      .delete()         )
    dbsession.flush()
    return jsonify(error=False)

Alternatively, you can look into setting up Foreign key relationships in your table statements if you use the ORM to create your tables

您的第二个示例有效的原因是您在检查表上指定查询并且仅使用在该表中找到的值。

对编辑的回复: Right now your table relationships aren't set up correctly.特别是以下部分:多对多从多对多表中删除行

此示例代码在发布链接中的更多(和更好)细节中进行了解释,但基本思想是您有一个associate_table(在您的情况下为StudentFavorites)包含具有在一个或多个中指定的关系的外键你的其他桌子。我个人建议您使用表示例而不是对象示例。

association_table = Table('association', Base.metadata,
    Column('left_id', Integer, ForeignKey('left.id')),
    Column('right_id', Integer, ForeignKey('right.id'))
)

class Parent(Base):
    __tablename__ = 'left'
    id = Column(Integer, primary_key=True)
    children = relationship("Child",
                    secondary=association_table,
                    backref="parents")

class Child(Base):
    __tablename__ = 'right'
    id = Column(Integer, primary_key=True)