I am building Library API project, and my code used to work just fine, but recently - when I aimed to finish working with it - I was checking my endpoints, and this is what I get when sending POST request:
{
"book_id":1,
"client_id":2,
"date_rented":"2023-07-06"
}
# ----------------------------------
{
"code": 422,
"errors": {
"json": {
"book_id": [
"Missing data for required field."
],
"client_id": [
"Missing data for required field."
],
"date_rented": [
"Missing data for required field."
]
}
},
"status": "Unprocessable Entity"
}
I checked everything from endpoint through Model and Schema, but everything seems to be fine. Below I sent the rest of related code.
endpoint:
@blp.route('/rental')
class ListBookRental(MethodView):
@blp.arguments(PlainRentSchema)
@blp.response(201, PlainRentSchema)
def post(self, rent_data):
book = BookModel.query.get_or_404(rent_data['book_id'])
book_rent = BookRentModel(**rent_data)
if book.status != "available":
abort(500, "You can not rent this book!")
else:
book.status = "rented"
book.times_rented += 1
try:
db.session.add(book_rent)
db.session.commit()
except SQLAlchemyError:
abort(500, "SQLAlchemyError occurred while inserting rent.")
return jsonify({"message": "Rent created."}), 201
model:
class BookRentModel(db.Model):
__tablename__ = "book_rent"
id = db.Column(db.Integer, primary_key=True)
book_id = db.Column(db.Integer, db.ForeignKey("books.id"), unique=False, nullable=False)
client_id = db.Column(db.Integer, db.ForeignKey("clients.id"), unique=False, nullable=False)
date_rented = db.Column(db.Date, unique=False, nullable=False)
date_returned = db.Column(db.Date, unique=False, nullable=True)
books = db.relationship("BookModel", back_populates="book_rents")
client = db.relationship("ClientModel", back_populates="book_rent")
schema:
class PlainRentSchema(Schema):
id = fields.Integer(dump_only=True)
book_id = fields.Integer(required=True)
client_id = fields.Integer(required=True)
date_rented = fields.Date(required=True)
date_returned = fields.Date(required=False)
books = fields.Nested(PlainBookSchema(), dump_only=True)
I checked the spelling, re-wrote request and nothing. I did research this, but all I could find was explanation of 422 status code. I compared this code to the rest of my project POST methods, and they're working fine. Maybe I don't see the obvious? Any help will be appreciated. Thanks in advance