This is the error I get: "sqlalchemy.orm.exc.UnmappedInstanceError: Class 'sqlalchemy.util._collections.result' is not mapped"
Objective:
On the "Ride" query_select_field I wanna show the destination concatenated with the car model and plate which are stored in Vehicle object. I need to submit and keep track of Ride.id. The working example below just show destination so if there is same destination served by two different vehicles I wouldn't be able to discrimminate.
The Form:
def list_rides():
a = Ride.query.join(Vehicle).filter(Vehicle.operator_id==current_user.id).all()
rides = (db.session.query(Ride, Vehicle)
.join(Vehicle, Vehicle.id == Ride.vehicle_id)
.filter(Vehicle.operator_id == current_user.id)
.all()
)
print(rides)
return a
class AddForm(FlaskForm):
ride_id = QuerySelectField(u'Ride',
validators=[DataRequired()],
query_factory=list_rides)
dep_date = DateField('Departure date', default=datetime.today, validators=[DataRequired()])
dep_time = TimeField('Departure Time:', default=datetime.now, validators=[DataRequired()])
max_seats= IntegerField('Max Seats:', default=8, render_kw={'enabled':''}, validators=[DataRequired()])
price = FloatField('Price:', default=9.0)
notes = TextAreaField('Notes:')
submit = SubmitField('Add schedule')
The code is working when I return "a" in the sense that Ride queryselectfield is populated with Ride repr . I cannot modify the repr object as I cannot call the Vehicle model which is part of a different Table/Model (Vehicle).
print(rides) output:
[(Monte Nudo, Ford Transit WCF556GP), (Monte Nono, Ford Transit
WCF556GP), (Peak Snow, Ford F350 CA 90210)]
but when I return "rides" instead of "a" in list_rides() I get the error above mentioned.
models.py
class Vehicle(db.Model):
__tablename__ = 'vehicles'
users = db.relationship(User)
id = db.Column(db.Integer,primary_key = True)
created_at = db.Column(db.DateTime)
make = db.Column(db.Text)
operator_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
plate = db.Column(db.Text, unique=True)
descr = db.Column(db.Text)
is_active = db.Column(db.Boolean)
def __init__(self, operator_id, make, plate, vehicle_description, is_active):
self.operator_id = operator_id
self.make = make
self.plate = plate
self.descr = vehicle_description
self.created_at = datetime.datetime.now()
self.is_active = is_active
def __repr__(self):
return f"{self.make} {self.plate}"
class Ride(db.Model):
__tablename__ = 'rides'
vehicles = db.relationship(Vehicle)
id = db.Column(db.Integer,primary_key = True)
created_at = db.Column(db.DateTime)
name = db.Column(db.Text)
vehicle_id = db.Column(db.Integer, db.ForeignKey('vehicles.id'), nullable=False)
notes = db.Column(db.Text)
def __init__(self, name, vehicle_id, notes):
self.notes = notes
self.vehicle_id = vehicle_id
self.name = name
self.created_at = datetime.datetime.now()
def __repr__(self):
return f"{self.name}"
The long story short is that I don't want to use repr of Ride to populate the queryselectfield but build my own.
Thanks.
question from:
https://stackoverflow.com/questions/65651831/flask-sqlalchemy-query-factory-usage-sqlalchemy-util-collections-result-i