2

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()})
1
  • "it will raise an error" -> please edit your question to include the complete error traceback. Commented Jan 9, 2021 at 12:21

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

It will raise again same error. The error is AttributeError: 'result' object has no attribute 'get_data'
Firstly thank you for your reply . return jsonify({'Devotee list': data.get_data()}) I think is there any correction in my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.