我有3个表,在遍历时遇到问题。它们包括一个CAR表、一个位置表和一个CARLOCATION表。汽车保持汽车数据,位置保持位置数据,并且配位数表位于中间,并将汽车链接到其当前位置。
使用Flask和SQLAlchemy,我希望用户能够通过ID搜索汽车,ID通过关系链接到CarLocation表,CarLocation表也通过关系链接到Location表。So Car->;位置->;位置
但是,当我调用Car.CarLocation.Location.Name时,我会收到一个错误,提示:
文件“D:\Documents\GitHub\car\u share\karshare\views.py”,第370行,在searchcar中 CurrentLocation=SearchedCar.CarLocations.Location.Name
AttributeError:“InstrumentedList”对象没有属性“Location”
任何能帮助我的人都是救命恩人
My models.py如下所示:
class Car(db.Model):
__tablename__ = 'Car'
__table_args__ = {'schema': 'KarShare'}
CarID = db.Column(db.Integer, primary_key=True, server_default=db.text(
"nextval('\"KarShare\".\"Car_CarID_seq\"'::regclass)"))
Make = db.Column(db.String(64), nullable=False)
Model = db.Column(db.String(64), nullable=False)
Year = db.Column(db.Integer, nullable=False)
Odometer = db.Column(db.Integer, nullable=False)
CarType = db.Column(db.String(64), nullable=False)
NumSeats = db.Column(db.Integer, nullable=False)
FuelType = db.Column(db.String(64), nullable=False)
CostPerHour = db.Column(db.Float, nullable=False)
TransmissionType = db.Column(db.String(9), nullable=False)
Registration = db.Column(db.String(), nullable=False)
class CarLocations(db.Model):
__tablename__ = 'CarLocations'
__table_args__ = {'schema': 'KarShare'}
CarID = db.Column(db.ForeignKey('KarShare.Car.CarID'), primary_key=True,
nullable=False)
CurrentLocationID = db.Column(db.ForeignKey('KarShare.Location.LocationID'),
nullable=True)
Location = db.relationship("Location", backref="CarLocations",
lazy=True)
Car = db.relationship("Car", backref="CarLocations",lazy=True)
class Location(db.Model):
__tablename__ = 'Location'
__table_args__ = {'schema': 'KarShare'}
LocationID = db.Column(db.Integer, primary_key=True, server_default=db.text(
"nextval('\"KarShare\".\"Location_LocationID_seq\"'::regclass)"))
Name = db.Column(db.String(64), index=True, unique=True, nullable=False)
StreetAddress = db.Column(db.String(128), nullable=False)
City = db.Column(db.String(64), index=True, nullable=False)
State = db.Column(db.String(64), index=True, nullable=False)
Longitude = db.Column(db.Numeric(precision=9, scale=6), nullable=False)
Latitude = db.Column(db.Numeric(precision=8, scale=6), nullable=False)
CarSpaces = db.Column(db.Integer, nullable=False)
以及导致错误的简单代码:
SearchedCar = Car.query.filter_by(CarID=form.search.data).first()
Make = SearchedCar.Make
Model = SearchedCar.Model
Year = SearchedCar.Year
Odometer = SearchedCar.Odometer
CarType = SearchedCar.CarType
NumSeats = SearchedCar.NumSeats
FuelType = SearchedCar.FuelType
CostPerHour = SearchedCar.CostPerHour
TransmissionType = SearchedCar.TransmissionType
CurrentLocation = SearchedCar.CarLocations.Location.Name
Registration = SearchedCar.Registration
谢谢你能给我的帮助
Car
通过关系有一个CarLocation
列表:相关问题 更多 >
编程相关推荐