【问题标题】:Trying to query and compare two columns from different tables with SQLAlchemy尝试使用 SQLAlchemy 查询和比较来自不同表的两列
【发布时间】:2014-09-10 20:50:33
【问题描述】:

我试图在 SQLAlchemy 的过滤查询中比较两个不同表中的两个值:

    response = db.session.query(Building, BuildingTypes)\
    .join(BuildingTypes)\
    .filter(x <= (Building.x+(BuildingTypes.width/2)))\
    .first()

我的问题是我似乎总是得到响应,即使这会导致无。我是不是做错了什么?

这是类定义:

class BuildingTypes(db.Model):
    __tablename__ = 'buildingTypes'
    id = db.Column(db.Integer, primary_key=True)
    width = db.Column(db.Integer)
    height = db.Column(db.Integer)
    title = db.Column(db.Text)

class Building(db.Model):
    __tablename__ = 'buildings'
    id = db.Column(db.Integer, primary_key=True)
    x = db.Column(db.Integer)
    y = db.Column(db.Integer)
    typeID = db.Column(db.Integer, db.ForeignKey('buildingTypes.id'))
    buildingTypes = db.relationship("BuildingTypes")

【问题讨论】:

    标签: python sqlalchemy


    【解决方案1】:

    问题在于,在上面的代码之前,x 是从 request.form 语句中检索的。因此(我想)它不被视为整数。因此,解决方案是用 int(x) 包装 x,现在代码可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      • 2016-02-22
      • 1970-01-01
      相关资源
      最近更新 更多