I am developing an API that uses Flask, flask_sqlalchemy, flask_marshmallow, and flask_restful to serialize and store data in the db.
I have about 15 Models and all of them work for except for this one:
class Student(db.Model):
__tablename__ = 'student'
studentId = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(50))
email = db.Column(db.String(50))
courses = db.relationship("StudentCourses")
connected = db.Column(db.Boolean)
def __repr__(self):
return "Student Id : {}, Name: {}, Email: {}, Courses: {}, Connected: {}".format(self.studentId, self.name, self.email, self.courses, self.connected)
# Marshmellow Schema
class StudentSchema(ma.Schema):
class Meta:
fields = ("studentId", "name", "email", "courses", "connected")
# Schema for Multiple Students
students_schema = StudentSchema(many = True)
# Schema for Individual Students
student_schema = StudentSchema()
class StudentListResource(Resource):
def get(self):
posts = Student.query.all()
# the posts are stored in the database but it only returns the name
for i in posts:
print(i)
return students_schema.dump(posts)
def post(self):
connection = False
if request.json['connected'] == "True" or request.json['connected'] == 'true':
connection = True
newStudent = Student(
name = request.json['name'],
email = request.json['email'],
connected = connection
)
db.session.add(newStudent)
db.session.commit()
return student_schema.dump(newStudent)
class StudentResource(Resource):
def get(self, studentId):
student = Student.query.get_or_404(studentId)
return student_schema.dump(student)
def patch(self, studentId):
student = Student.query.get_or_404(studentId)
if 'studentId' in request.json:
student.studentId = request.json['studentId']
if 'name' in request.json:
student.name = request.json['name']
if 'email' in request.json:
student.email = request.json['email']
if 'connected' in request.json and (request.json['connected'] == 'True' or request.json['connected'] == 'true'):
student.connected = True
db.session.commit()
return student_schema.dump(student)
def delete(self, studentId):
student = studentId.query.get_or_404(studentId)
db.session.delete(student)
db.session.commit()
return '', 204
# Registering the Endpoints
api.add_resource(StudentListResource, '/students')
api.add_resource(StudentResource, '/students/<int:studentId>')
When running an insertion using:
curl http://localhost:5000/students \
-X POST \
-H "Content-Type: application/json" \
-d '{"name":"dean", "email":"test@dean.com", "connected":"True"}
It inserts the whole row but only returns the name field:
{
"name": "dean"
}
The correct output should look like this:
{
"studentId": "1", "name": "dean", "email": "test@dean.com", "connected":"true"
}
I am not sure why this is the case because I have a nearly identical schema called Teacher that returns and inserts the whole row correctly. It looks like this:
# TEACHER
class Teacher(db.Model):
__tablename__ = 'teacher'
teacherId = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(50))
email = db.Column(db.String(50))
courses = db.relationship("TeacherCourses")
connected = db.Column(db.Boolean)
def __repr__(self):
return "Teacher Id : {}, Name: {}, Email: {}, Courses: {}, Connected: {}".format(self.teacherId, self.name, self.email, self.courses, self.connected)
# Marshmellow Schema
class TeacherSchema(ma.Schema):
class Meta:
fields = ("teacherId", "name", "email", "courses", "connected")
# Schema for Multiple Teachers
teachers_schema = TeacherSchema(many = True)
# Schema for Individual Teachers
teacher_schema = TeacherSchema()
class TeacherListResource(Resource):
def get(self):
posts = Teacher.query.all()
return teacher_schema.dump(posts)
def post(self):
connection = False
if request.json['connected'] == "True" or request.json['connected'] == 'true':
connection = True
newTeacher = Teacher(
name = request.json['name'],
email = request.json['email'],
connected = connection
)
db.session.add(newTeacher)
db.session.commit()
return teacher_schema.dump(newTeacher)
class TeacherResource(Resource):
def get(self, teacherId):
teacher = Teacher.query.get_or_404(teacherId)
return teacher_schema.dump(teacher)
def patch(self, teacherId):
teacher = Teacher.query.get_or_404(teacherId)
if 'teacherId' in request.json:
teacher.teacherId = request.json['teacherId']
if 'name' in request.json:
teacher.name = request.json['name']
if 'email' in request.json:
teacher.email = request.json['email']
if 'connected' in request.json and (request.json['connected'] == 'True' or request.json['connected'] == 'true'):
teacher.connected = True
db.session.commit()
return teacher_schema.dump(teacher)
def delete(self, teacherId):
teacher = teacherId.query.get_or_404(teacherId)
db.session.delete(teacher)
db.session.commit()
return '', 204
# Registering the Endpoints
api.add_resource(TeacherListResource, '/teachers')
api.add_resource(TeacherResource, '/teachers/<int:teacherId>')
This schema can be to and returns correctly as:
curl http://localhost:5000/teachers \
-X POST \
-H "Content-Type: application/json" \
-d '{"name":"brian", "email":"test@dean.com", "connected":"True"}'
{
"teacherId": 2,
"email": "test@dean.com",
"name": "brian",
"courses": [],
"connected": true
}
Any help is greatly appreciated!