swaroop gannamani
swaroop gannamani

Reputation: 43

How to get data from multiple tables using flask-sqlalchemy

Here is my tables.

class maindevotee(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(225))
    phonenumber = db.Column(db.String(225))
    gothram = db.Column(db.String(225))
    date = db.Column(db.String(50))
    address = db.Column(db.String(250))

    def json(self):
        return {'id': self.id, 'name':self.name, 'phonenumber': self.phonenumber, 'gothram': self.gothram,
                'date': self.date, 'address': self.address}

class relatives(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    main_id = db.Column(db.Integer, db.ForeignKey('maindevotee.id'), nullable=False)
    name = db.Column(db.String(225))
    star = db.Column(db.String(225))
    gender = db.Column(db.String(45))
    relation = db.Column(db.String(45))

    def json(self):
        return {'main_id': self.main_id, 'name': self.name, 'star':self.star,
                'gender': self.gender, 'relation': self.relation}
    
class services(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    main_id = db.Column(db.Integer, db.ForeignKey('maindevotee.id'), nullable=False)
    pooja = db.Column(db.String(225))
    god = db.Column(db.String(225))
    price = db.Column(db.Float)
    donation = db.Column(db.String(225))
    booking_fromdate = db.Column(db.String(50))
    booking_todate = db.Column(db.String(50))
    prasadam = db.Column(db.String(225))

    def json(self):
        return {'main_id': self.main_id, 'pooja': self.pooja, 'god': self.god,
                'price': self.price, 'donation': self.donation, 'booking_fromdate': self.booking_fromdate,
                'booking_todate': self.booking_todate, 'prasadam': self.prasadam}

How to get data from multiple tables in a single request. Here is my scource code to join the three tables. If i am try to get data from database it will raise an error. and the error is AttributeError: 'result' object has no attribute 'get_data' can i get the data from database using foreign key.

data = db.session.query(maindevotee, relatives, services)\
    .filter(maindevotee.phonenumber == 3251469870)\
    .join(relatives, maindevotee.id == relatives.main_id)\
    .join(services, maindevotee.id == services.main_id)\
    .first()

def get_data():
    return [data.json(get) for get in data.query.all()]

@app.route('/getdata/<phonenumber>',methods=['GET'])
def getdata():
    return jsonify({'Devotee list': data.get_data()})

Upvotes: 2

Views: 3103

Answers (1)

Nour-Allah Hussein
Nour-Allah Hussein

Reputation: 1449

Correct

data = db.session.query(maindevotee, relatives, services)\
    .filter(maindevotee.phonenumber == 3251469870)\
    .join(relatives, maindevotee.id == relatives.main_id)\
    .join(services, maindevotee.id == services.main_id)\
    .first()

to

data = db.session.query(maindevotee, relatives, services)\
    .filter(
    (maindevotee.phonenumber == '3251469870')
    & (maindevotee.id == relatives.main_id)
    & (maindevotee.id == services.main_id)
    ).first()

for more clarifications, ask in the comments.

Upon comment

in

@app.route('/getdata/<phonenumber>',methods=['GET'])
def getdata():
    return jsonify({'Devotee list': data.get_data()})

data contains the query results, that do not include the function get_data(), therefore you face the mentioned error.

Try the following modification, I think this is the result form you may want:

@app.route('/getdata/<phonenumber>',methods=['GET'])
def getdata():
    return jsonify({**data.maindevotee.json(),**data.relatives.json(),**data.services.json()})

Good Luck

Upvotes: 3

Related Questions