0

I am using a flask API as my rest point for my Angular application. Currently I am testing the API. I tested my /users point to make sure I got all the users.

//importing db, app, models, schema etc.
from flask import jsonify, request

@app.route('/users')
def get_users():
    # fetching from database
    users_objects = User.query.all()

    # transforming into JSON-serializable objects
    users_schema = UserSchema(many=True)
    result = users_schema.dump(users_objects)

    # serializing as JSON
    return jsonify(result.data)

That worked. However, now that I am trying to get other data(which has more than 9000 objects.. it doesn't work(when I try querying all of them). I first just grabbed the first item

@app.route('/aggregated-measurements')
def get_aggregated_measurements():
    aggregated_measurements_objects = AggregatedMeasurement.query.first()

    # transforming into JSON-serializable objects
    aggregated_measurement_schema = AggregatedMeasurementSchema()
    result = aggregated_measurement_schema.dump(aggregated_measurements_objects)

    return jsonify(result.data)

That showed me the first AggregatedMeasurement. However when I try to query all of them aggregated_measurements_objects = AggregatedMeasurement.query.all() Nothing displays. I did the same thing on my jupyter notebook and that displayed them. I then thought that maybe this was too much info, so I tried to just limit the query like this aggregated_measurements_objects = AggregatedMeasurement.query.all()[:5]. That works on the jupyter notebook, but displays nothing when I hit the route.

I don't understand why when I hit the /users point I can see all of them, but when I try to do the same for aggregated-measurements I get nothing(even when I limit the query). I am using flask_sqlalchemy with sqlite db.

**update with model and schema **

from datetime import datetime

# ... import db
import pandas as pd
from marshmallow import Schema, fields

class AggregatedMeasurement(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    created = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)

    time = db.Column(db.DateTime, nullable=False)
    speed = db.Column(db.Float, nullable=False) 
    direction = db.Column(db.Float, nullable=False)  

    # related fields
    point_id = db.Column(db.Integer, db.ForeignKey('point.id'), nullable=False)

    point = db.relationship('Point',backref=db.backref('aggregated_measurements', lazy=True))                                        

class AggregatedMeasurementSchema(Schema):
    id = fields.Int(dump_only=True)

    time = fields.DateTime()
    speed = fields.Number()
    direction = fields.Number()
    point_id = fields.Number()

SECOND UPDATE found the error. After verifying that indeed it was hitting the db( thank you @gbozee) I noticed that on the /aggregated-measurements route when I made the schema I did it for just one object. I forgot to include the many = True like I did in the users_schema. Therefore that is why only one point appeared and when I tried more, it did not. I was using the marshmallow(an object serialization package).

nrvaller
  • 353
  • 6
  • 18
  • could you paste the `AggregatedMeasurement` model so as to provide more information. – gbozee May 02 '18 at 08:27
  • also if you are familiar with `pdb` in python, you could put a breakpoint immediately after `aggregated_measurements_objects = AggregatedMeasurement.query.all()` in your view to ensure that a call to the db is being made `aggregated_measurements_objects = AggregatedMeasurement.query.first();` `import pdb; pdb.set_trace()` – gbozee May 02 '18 at 08:30
  • @gbozee I updated the post. No, I am not familiar with `pdb` in python but I will try that. Thank you – nrvaller May 02 '18 at 08:38
  • 1
    Sinmce it is difficult to format code in the comment section, i created a gist displaying what you need to do. https://gist.github.com/gbozee/cb516b022ba4fbc865905fc38702c5b5 – gbozee May 02 '18 at 08:43
  • Once the breakpoint was set, I checked `aggregated_measurement_objects` and it gave me the object with the corresponding info from the db for that particular object. @gbozee – nrvaller May 02 '18 at 09:25
  • I just checked with ` aggregated_measurements_objects = AggregatedMeasurement.query.all()` and saw all of my objects on my console... hmm. It is hitting my db but when I hit the point, it's empty(just for aggregated-measurements, for users I do see everything at `/users`. I'm going to double check my routes. – nrvaller May 02 '18 at 09:33

0 Answers0