在下面的设置中,如何从汽车的所有轮胎中检索一组传感器数据?你知道吗
一辆车有很多轮胎,而一个轮胎只有一个传感器数据,所以在SQL中我会做如下的事情
SELECT sd.*
FROM Car AS c
INNER JOIN Tire AS t ON c.id = t.car_id
INNER JOIN SensorData AS sd ON t.id = sd.tire_id
class Car(Base):
pass
class Tire(Base):
car = relationship("Car", backref="tires")
class SensorData(Base):
tire = relationship("Tire", backref=backref("sensor_data", uselist=False))
我猜有一个更优雅的方式获得所有传感器数据的比下面,但我找不到它。你知道吗
tires = Car.query.get(1).tires
sensor_datas = [sensor_data for tire.sensor_data in tires]
有谁能建议一个更好的方法吗?我在想我应该可以做
sensor_datas = Car.query.get(1).sensor_datas
OR
sensor_datas = Car.query.get(1).tires.sensor_data
这些解决方案将在
Car
上返回一个只读的sensor_datas
属性,有关详细信息,请参阅注释:输出:
相关问题 更多 >
编程相关推荐