I have the following data in a table named shippers.
id companyname phone
1 Company A 3105590430
I'm trying to extract the data in both columns using SQLAlchemy. I'm able to extract the data in the column labeled companyname but not the column labeled phone.
Here is the code I'm using.
class ShippersModel(db.Model):
__tablename__ = 'shippers'
id=db.Column(db.Integer, primary_key=True)
companyname = db.Column(db.String(40))
phone = db.Column(db.String(24))
def __init__(self, companyname, phone):
self.companyname = companyname
self.phone = phone
def find_by_name(cls,companyname):
column = cls.query.filter_by(companyname=companyname).first()
print(column.companyname)
print(column.phone)
The method find_by_name returns the value as expected for column.companyname. However, the column.phone returns the value None. Any thoughts on the reason the value None is being returned?
I'm using Postman and the GET method to test the code. The find_by_name method is first called from the GET method as shown below.
from flask_restful import Resource, reqparse
from models.shippers import ShippersModel
class Shippers(Resource):
parser = reqparse.RequestParser()
parser.add_argument('phone',
type=str,
required=True,
help="This field cannot be left blank"
)
def get(self,companyname):
shipper = ShippersModel.find_by_name(companyname)
print(shipper)
if shipper:
#return {'message': 'Shipper found'}
return shipper.json
return{'message': 'Shipper not found'},404