More Database And Schema Relationships - FastAPI Beyond CRUD (Part 15)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everyone welcome back in this video going to be continuing from where we stopped in the previous video went ahead and established relationships between the existing tables or the existing entities that we have within our application and those are the users as well as the books our logic was that a user could make a book submission and then we would keep track of the specific user who submitted that book and that's what we did in the previous video we also looked at how we could retrieve a user's book submissions and how we could return that relationship using the pantic models So today we're going to be continuing by building a more complex relationship and that is going to be one to link every user and every book to a review so user will be able to submit a book review and we shall keep track of the book they made the review on as well as the comments they made and all that stuff but before I go there I noticed that we have an error in our code so if you go to how we create a user account when you try to create a user account this user is going to be created in our database but it's going to return an internal server error and this server error is being returned because this user has not been serialized in the way we want so if we go back to our code that is going to create our user account we have all the logic working except that for returning our newly created user so if we go to our terminal and see what is wrong we shall notice that we still get that missing green late error so the missing green late error is one that is raised each time we try to operate outside the context of green l so a green L is simply a simple unit of execution just like you see a thread but SQL Alchemy uses these to perform queries and therefore if we operate outside the green light spawn context then we are going to get such errors and that's what it means when we get that error so we're trying to serialize that but remember this model is also getting the list of books and that is going to make another extra query to our database to get all those books so for us to fix that we need to make sure that the where we are serializing this user for this case is going to be in a way that is not going to return those books because we will be operating outside green latest context so if you have not understood that I'm going to of course leave some description or some explanation in the description so that you'll be able to understand what it does so to fix that what we're going to do is to Simply modify our user model so that it doesn't return the books or we shall create a separate model that's going to return all information about the user as well as the books that are associated with a user so to begin I'm going to create a class and I'm going to call this class our user books model so this is going to inherit from this class because what we want to do is to go ahead and inherit everything that this class has including all the attributes methods and so on so to inherit all of them we shall say that this is going to inherit from our user model and once we've done that we've created a class that has all these fields as well as the one to return the list of books that are associated with a user so to use that we are now going to leave our sign up route or sign up path to use the response model which is our user model and then that one that Returns the user and box which is get current user for that we shall use our user books model so we're going to go at the top right here and I'm going to start by importing our user book model or our user books so let me make it more clear say user books and when you go back here we shall also rename this to user books so once that is done if we try to to go back and let's say try to create a new user account so I'm going to provide another user uh let's say this is is going to be um Jennifer and then uh we shall provate their email as let's say J 123 and then uh let me actually also use gen 123 for their usern name and then we can provide their name as Jennifer so and then there let's say Jennifer Smith or whatever name we may have so if we try to create a user account in this case we shall see that their user account has been created successfully and when you try to uh create a new token so let me try to create a new token for them so I'll just get the email and password and then we shall go to where I create a token pair so this is going to be at gmail.com and then our password is our user is going to be J 123 if we try to send in this case shall get their access and refresh token and when I get the access token I'm going to try to get the current user so I'm going to go to the authorization header and then I replace the current token with this new token so in theend we can get the user and for our case we need to make this return the reviews and the books that this user has made so we need to go ahead and set the response model so that it does that so we have our user model in our case we shall say that this is going to be our user books model so once that is done it's going to return an empty list of books just like you see right here and now we're also going to work on our reviews so that we shall return the detail of the currently logged in user their books and all the reviews theyve made on every book now that You' been able to to look at how to fix that let us look at our current database structure so our current database structure simply has the users and our books table but we want to go ahead and make sure that every user can add a review to our book and therefore we shall modify our database structure to look something like this so in this database structure we see that we have users having a on to relationship to reviews therefore one user can make as many reviews as they want but they'll also be making reviews to a book and therefore we have this Association to The Book Table so a book will also have as many reviews as possible so one book will have many reviews and one user will also make many reviews so you have a one to many relationship between books and reviews and also a want to many relationship between our users and our reviews so let's go ahead and implement this the first thing we're going to do is to go within our code and we're going to move our modeles to a common module or to one module in which it will be access throughout our application now for a simple app like this if you do not want to get into cular inputs in future then it will be wise for us to have all our code Logic for model SED in one place and the reason that's why I'm going to do this is because we are going to have relationship attributes that will need for us to refer to other database models outside the modules in which we shall be creating them so to do that I'm going to first of all create the module so I'll go right within our source folder and within our DB I'm going to create a new file which I'm going to call modpi so all our models are going to be located in here and once I'm done with that then I'm going to start moving our models that are existing within our application I'm going to start with our user model I'll simply go ahead and provide it right there and then I'll do the same thing for our or so I'm going to go ahead and simply delete this o models file since we've moved its code inside model.py another thing I'm going to do is to go ahead and do the same thing for our books so I'll go to books. mod and then I'll simply cut the code from there and then I'll paste it inside here so once that is done then the next thing I'm also going to do is to make sure that these inputs are at the top and we're also going to look at how we shall fix our file so that we do not have them being repeated like the way we have them one thing we can do is just actually remove the repeated inputs just like you see right here and if you have any unin then we shall simply go ahead and fix them so I'll come right here and the first thing is to import dat because that is appearing as undefined so when you go to the bottom of the file we still have the optional type which you did not import so I'll go at the top and also import it so I'll sa from typing import list but shall also import optional and once that is done we do not have any undefined now the next thing is to go ahead and fix everywhere we imported this within our code so I'm going to use vs code search feature and I'm going to look for all models that exist within our code so right in here we have source. books. mods but we now know that all our models are being got from source. db. mods so I hope I'll go ahead and fix that with from source. db. mods import book and I'll do the same thing for our model so I'll just come right here and I'll just simply say um Source dot this is going to be DB do model we shall import our user and then I'll do the same thing here I'll say source. DB do models which shall import our user and then I'll do the same thing here I'll say source. DB do models and that will go ahead and fix all the input errors that we have within our code so I'm going to go ahead and save everywhere I made that change and then we can now go ahead and test to see if our server is running so I pull up myal right here I'm seeing sour. o so think that is on models so I guess I have to remove this or save and now server is running what I'm going to do is to go ahead and remove the models that is inside our boook so I have to get rid of it just like I did for o and now we have our models residing inside our DB folder so it seem like I still have another input issue so right now we have this uh these inputs right here for basically the table relationships that we set up in the previous video and we going to fix this is simple since we have all these tables or models located inside One models folder it can be as simple as just say instead of saying DB mod or Source mod or modbook we can just say book and then for our case if want to call a user model since this is already defined within our current module can just say user then we can go at the top right here and remove where we imported our model so in this case I'll just simply go ahead and remove this and that will just be enough for our server to run again now that our server has run let us go and ahead and create our reviews table so to create our reviews table we are simply going to go down here and we're going to create a class which we're going to call our reviews so this reviews class is going to be similar to what we have at the top right there so if you to look at our database structure we have this having the book ID or the book U ID which is going to be referring key to the ID of the users as well as the referring key to the ID of our books so you're going to go ahead and set this up but we also need the review text as well as the rating for that specific book that a user has submitted so I'm going to go down here and I'm going to Simply Define this basing on what we have I can go ahead and copy this entire class and then I'll paste it right here then instead of book we shall have this being our review class and it will have all these attributes but the table name will be reviews in of books another thing we shall have the same uid so we need to just go ahead and remove some of these other fields that are going to exist on our table and in this case we shall have the first one being our rating so the rating will be an integer but we can go ahead and also keep this integer validated so we can use the field function to do that and set up our Max rating let's say our Max rating is going to be five and therefore the top most rating will be five or let's say five stars and stuff like that another thing we can do is to set up the user U ID so the user uid will be an optional uid but that optional u u ID will be aering to user. ID and I think that is correct another thing we shall set up is going to be the book U ID so our book U ID is going to be set up in a similar way but in this case it will have the name of our book uid and then instead of being a fory to use as uid in this case it will be a fank key referencing book or books. uid another thing we've missed I think is going to be a review text so we need to add that by saying review uncore text and that will be a string so this simply shows that this review text will be a string and it will not be nullable and that is just enough for us so we have now created our VI table all we are left with is is simply modifying this method right here which is simply want to return a string representation of any object that we create out of this class so I'll just come right here and say that our review so in this case we can say that this is going to be a review and this preview is going to be for self dot so in this case we can just come and say uh self dot uh self. book or in this case we can just say book uid and then we can also say buy the user who created that which is going to be self dot so in this case we can just say self review by book for book that and then by user and then you can have self. user [Music] uid and this will be just enough to do that so I can go ahead and fix this and then say for book self. book uid and then by self user ID so our review database model has been created so let's go ahead and use alic to set this up in our database so to begin I'm going to first pull up my terminal I'll go to the B so I guess I have to create a new terminal right here and then I'll run alic our case we're going to create a new revision and this new revision is going to have to be autogenerated from our model so I'll say autogenerate and for our case we shall basically give it a message describing what change we have done so our change is going to be add review table so when you press enter this will go ahead and generate a migration or the revision so if you go ahead and check this out we're going to notice that it's going to create the reviews table or the details about the review table and how we're going to apply them to our database to make sure this reflect within our datab B we're going to go within our code or within our terminal and run almic so this is going to be alic upgrade head and then this changes will be done in our database so we've been able to set that up once our table has been created we can confirm whether it has been created by simply going back to this graphical user interface right here I'm going to just simply reconnect to the database and now in our public right here we can see public. reviews being added so when you enter right here we can see the different columns that we have set up on our database table all right now that that is out of the way let us also set up our relationship attributes just like we did for our users and our books so to begin we are going to first of all set up the user relationship attribute that's going to allow us to access the user who created a certain review so to begin uh we're going to modify this space saying that this will be a user object it's going to be an optional user object so in case one does not exist then that will not be returned and the relationship will back populate to so in this case we have to modify this to make it reviews instead of books and once that is done then we are going to do the same thing on our user Side by introducing the reviews list that is going to be the reviews that our user has made so when you go back to our user model right here we're going to do something that is very similar to what we have on our books list so I'm going to go right here and I copy this relationship attribute I past it just below the books relationship attribute but I'm going to rename this to reviews instead of books and once that is done the next thing is to also specify that this will be a list of reviews so I'm going to go ahead and remove the book model and replace it with our review model so keep in mind that we set this up the back plate in this case is going to be our user so that will be just exactly how we set it up the user will be what this relationship attribute will back populate to once that is done then let us go ahead and look at how we can set up the relationship attribute for accessing the book that that review was left so I'll just come right here and say book and this book is going to be an option book object so once we have that we can now come and basically I do the same thing like we did here I'll just come right here and say that our relationship is going to be pointing to reviews and this reviews is going to be found on our book model so I'm going to copy something very similar to what we have here and in now book model I'll add that relationship attribute that is simply going to allow us to access the reviews that were left on a book so in this case we're going to come and specify that this is going to be a list of reviews and this relationship will be back prating to the book so this mapping will be on to our book just like we set up this book relationship to map to our review on our Book Table our book relationship so once we done with this now it's time we created the API endpoint for basically adding a review to our books to do that I'm going to go within our reviews folder or our reviews package so we don't have one yet but I'm going to create one by saying new folder so we shall call this our reviews folder inside that folder we are going to go ahead and basically add a in ity to mark it as a package and once we are done with that we shall create both our models actually not our models but shall create a service class that's going to allow us to basically create code that is going to be uh for the card or for any functionality in our endpoints for reviews so inside that I'm also going to create the routes. py and this is going to be where our code for the API endpoints or the API Parts is going to be found the first thing we're going to do is to create the review router so we're going to create an object of the API class or the API router class and to do that we shall first import it with from first API we shall import our API router once we've imported our API router then we're going to create the review router so this review router is going to have all RS that are basically related to reviews and all that stuff so shall just create that right here so we need to also go ahead and register this within our applications our close some of the fils that we have right here and once that is done we shall move to our init.py inside that Source folder we shall import that router by saying from so this is going to be from Source dot in this case we shall have our reviews routes we are going to go ahead and import our review route and once that is done then we are simply going to go ahead and include that router shall just come down here and say app. include router so we're going to pretty much have almost everything that we have here except that shall rename it to our review router so we're going to name this our review router and once we are done with that we shall have our prefix being API version and then slash reviews and then our tag is going to be the review tag we shall look at this when we looking at our API documentation with swagger and redoc in the later video so once that is done now let's go ahead and write the code for creating a new review so to create a new review We Shall go within our service. pii class the first thing we shall do is to import our database model for our review so we shall do that by saying from source. db. models we shall import the review model and once you've imported that remember that all the functions or all methods for carrying out cred on both our users and our books is going to be found within the their respective service classes so also going to go ahead and import those so we shall say from source. o. service we shall import the user service and once that is imported we shall also import the book service with source. books. service we are going to import our book service so this is going to be our book service and once that has been done now the next thing is going to be for us to create the service class for our review to do that we shall say the our class is going to be called the review service and this review service is going to have our first method being that to basically create a new review so we shall just simply call this add new review or add review to book something like that so this is going to take in the details of the user who has made the review the details about the book that we are reviewing as well as the session that's going to be used to make that specific query or make those queries to the database so to begin we're going to provide our user uid which will be a string or let's actually use the user email since we have the method for getting them by email and another thing we can do is to go ahead and provide the book U ID as that is the ID of the book we are going to try to basically make a review of so in this case we shall have our book Q ID and then we shall also have our session so the session is going to be an asnc session object so you have to import the asnc session class so I'll say from SQL modelex do async iio we shall import the async session class so this is actually supposed to be found within the do session module I go ahead and say import our async session once we've imported async session now one thing that we also need is going to be the data that will be used to create that specific review or the review text and the review ratings so let's just add that right before the session so to do that I'll just simply come right here and say that our review data is going to be of type so we need to basically set up the schema that's going to allow us to uh describe the data that will be for creating a review now we shall just simply call this the review create model so we need to also go ahead and create this so it doesn't really exist so we need to go ahead and set it up now I'll just come below right here and pass now one thing you may for May realize is I forgot to name this as syn Def and that is a mistake that I do a lot so I'm just simply going to come here and say pass and then I'll format it a little bit to look more organized but we need to create this schema so I'll just come right here copy this name and then do something like from source schemers we shall import our review create model so I'm going to go to our reviews and then I create it create a new file which is going to be our schemers py and this schemers py is going to have the class that's going to be responsible for creating our reviews so I just come right here and say from pantic we shall import our base model class and once we've imported it we are now going to have a class that's going to be our review create model that you're going to pass this for now before we create our review create model we also going to have our review model now this is going to be the model for basically helping us to get or retrieve wherever we shall be in position to retrieve and serialize a review object so this is what we shall may maybe use so we're going to go ahead and get the model for our review and then copy the fields that we have right here so I'm just simply going to go ahead and get these fields and add them so that they can be part of the schamer so instead of having these other field details I'm going to go ahead and remove them and that's is going to be just enough to set up our review model so once this is done now let me go and set up the two fields that we shall need to create a review those fields are going to be our rating as well as our review text so I'm just going to copy them and then paste them inside our class so this is going to be what we shall have in here sorry for that once we are done with that now let's go ahead and import that from here so this is going to be our review create model and this is going to be from dokas since we are in the same file so shall say from dokas we are going to import our review create model once that is done now let's go ahead and write the logic for helping us to add a review to a book now it has been my mistake to go ahead and write the service class without using TR cach statements and that is that has been problematic as you've been seeing so I'm going to just Begin by using a try cat so I'll say try and then I'll accept whatever exception shall be getting so I'll just write an exception right here and I'll accept every error which are getting for this service classes and then I'll just raise an HTTP exception I'll just go ahead and UT that class from Fast API so I'll say from Fast API do exceptions in this case we shall import the HTTP exception class and once that has been imported then we are going to go ahead and raise the HTTP exception and that will be a server side error so I'll say status code status code for ACC will be our St as to http 500 so I have to also go ahead and import that first API import status all right so let's go ahead and set the status app so our status is going to be HTTP 500 server error and then our detail shall set a detail to just let the user know that something has been wrong so shall say something like oops H something went wrong and that is going to be what you shall have so what you're going to try to do is to basically go ahead and add a review to a book so to begin we shall first of all get that book for which user wants to make a review so to do that shall create a variable called book and then since we have our user service and our book service we shall make those use of those to allow us to get the objects that we want to go ahead and add reviews for so I'm going to come right here and create a book service object by saying book service and our book service is going to be our book service so just going to come and say book service and then this will be the same thing for our users so we shall say user service is going to be equal to our user service and once that is done now let's go ahead and try to use our book service to get the book so shall say await we created a book service object then in this case we shall call the get book method so the get book method is the one that's going to allow us to get the book we shall specify that our book uid so in this case shall use the book uid and that book Q ID is going to be the book Q ID we provided within our function within our method right here then we shall also need the session so our session object is well is the one that we're going to use for this case once that is done now let's us also get the user who is currently making that request so to get the user we shall just come right here and say that our user is going to be equal to a wait uh user service do get user by email we shall provide the email as the user email and then we shall also try to get the session so our session is going to be equal to the session all right so we've created we've got both our user and our book now let's go ahead and create the new review that we want to go ahead and associate with these two objects to create the new book we shall just come right here and say new book actually new review this is going to be our new review so our new review is going to be uh an instance of the review class and once we've created that instance now we're going to go ahead and unpack the data that we shall get from our review data so to do that we shall just come right here and say as so that is basically saying unpack as a dictionary or unpack the dictionary as a list of keys and values so in our case we shall say that this is going to be our reviewed data now remember this reviewed data is going to be uh this review data is going to be in form of an object so to unpack it as a dictionary we shall call the model Dum this is going to be the model Dum method and this will go ahead or to make it simpler we can just come right here and create a new variable so this variable can be something like review data and then dictionary and this will be the value of that then we can unpack the review data dictionary so once that is done now let's go ahead and basically make sure that this review has both the user and book associated with it so to make sure that a User submitted a book we shall just simply update the new review object by saying new review do user and for our case this is going to be our user user that we've created right here so once we've been able to do that then let's do the same thing for our book so we're going to associate this review to the book we shall just come and say new book Dot and in this case we can simply go ahead and say new book. book or new review. book I don't know why I'm so C up in books so this will be new review. book and we shall associate it to the current book once that is done now let's use our session to commit this to our database we shall say session do add and then we shall add our new review instance and once we've been able to do that we shall await our new our session do so shall call session commit but since this a and therefore we have to call it with await so I'll say await session. commit and for our case we shall just simply return the new review so shall just come right here and say return new review and just like that we've been able to create the method that's going to be responsible for allowing us to add a new review to a book we need to go ahead and create the route or the API path for adding a review to a book that's what we have not yet done so we need to go to routes. P inside our reviews folder and the first thing I'll do is to make use of this review router object so I'll say at review so this is going to be at our review router in this case we shall say that this is going to be a post method then we shall be adding a review to a book so this is going to be on review slash book slash and we shall add the book Q ID so I'll provide it here once I've been able to provide it I'll simply go ahead and Define the r hander for it so I'll say I sync Def and this will be add review to book now let me close what we have here so once I've been able to add a review to a book or Define the function as ADD reviews to a book I'll simply go ahead and provide the different things that we shall need and for our case it's going to be the book U ID so the book uid in our case is going to be a string and then we shall also provide the user uid or the user who is actually adding this review now to prevent doing this we can actually access this user using the get current user dependency that we defend in the previous videos so I can just come right here and say current user is going to be a dependency but this is going to be of our user type of our user class so I can go ahead and import this by saying from source. DB do model we can have our user and once we've imported our user then we simply going to come right here and say import our user once you've been able to create or once you've been able to do that once you have been able to do that then the next thing we going to provide is going to be the data that will be used to add this review so shall just come right here and say that shall have our review data and our review data is going to be of type review create model that we created within our schema file so going to say from schemas we shall import the review create model and once we've imported that we're going to provide it right here then we shall also need our session object now remember the session object was created inside source. db. main so we have to also go ahead and import that so we shall say from source. DB main that's why we going to import this session which is called get session just to help us be able to access the session object so our session object is going to be of type as sync session and if we need it we shall just come right here and say from SQL model dot EXT do iyn session or I syc IO do session we shall import the async session class once we imported it we shall provide the type it is going to be a sync session and then here we shall inject our dependency so to inject our dependency we're going to need to have access to that depends function so this depends function is going to be the one that's going to help us get the dependency or get our session so we shall say get session once we've been able to do this now we're going to come right in here and the first thing we going to do is to create the new review so we shall say new review is going to be equal to so for our case we need to import the review service so we shall say from service we shall go ahead and import our review service and once we've imported the review service we shall create the object by calling it our review service once we've been able to do that then we're going to make use of that object by saying review service and in this case we shall say review service do add new review to book and for our case we shall need all these things so remember that this is UNA awaitable therefore we have to go ahead and use await to call it once that is done we shall provide our user email so we shall say our user email is going to be equal to the current user email now remember this is going to be a type of of object that shall get from the dependency which we forgot to do so we have to also go ahead and import that so that dependency exists within source. o. dependencies and that's why we shall get the get current user dependency so once we've got that then we're simply going to go ahead and inject it by saying equals and this will be depends and then we shall provide our get current user now notice that we have an error here and that is because this n default argument follows a default argument so you have to go ahead and fix it just go ahead and make it exist just before the ones that have current values all right so once we've been able to do that let's go ahead and get the current user email so we shall say this is going to be current user. email and then we shall go ahead and provide the review data so our review data is going to be equal to the review data and then we shall also for get the book Q ID so our book Q ID or the ID of the book want to add the review to you simply going to also be the book uid then in here we're also going to provide the session object and this session object is going to be the session object of our current path Handler so once that is done now we shall just go ahead and simply return our new review so we shall say return new review one other thing that we can do is to go ahead and simply check uh within our service so basically what we have here is a call to the get book function so just in case this book does not exist we need to be in position to return n or check if the book is not existing within our database so to do that what we can say inside our service here is to basically go ahead and create the book If the book actually exists so what we're going to do is to just simply go ahead and make that check inside here so new book new new review book is going to be equal to book but this has to only happen when the book is not n once this checks pass our user is going to be not n and also our book will not be n and therefore we shall just go ahead and assign them to the review object and go ahead and save so that's just something that I remember to do once we've been able to do that now let's go ahead and test this out in rest Fox so right here in rest what I'm going to do is to first of all create a new folder and inside this folder I'm going to name it to reviews and this is where all the request for our reviews are going to be found I've actually created the new request so to fix that I'll go ahead and create a folder which I'll call reviews and inside here is where all these reviews are going to be found or all API endpoints for reviews are going to be found now let us begin my testing that API endpoint for creating a new review or adding a review to a book so I'm going to go ahead and simply get one of this or get the root end point and then that's what I'm going to be using so I'm going to close some of the stuff I have here and then I'll come right in here so I'm going to provide our URL so this is going to be/ reviews and then our path is going to be one for slash book SL uid so I'll just come right here and I provide this path which is going to be one for basically providing our book U ID so I'm going to get the book U ID from where we list all our books so just going to come right here and try to get a new book so it seem like I have a problem on that server so we now see that we have a first API error so invalid act for response field check that the class reviews. schema. create model is a valid pantic field type if you're using a return type annotation this is so it seem like we made a mistake when defining the end point so let's see what we have here so this error arose from our schemas P simply because we forgot to actually go ahead and Mark this as a pantic base model now forgive me for this what I'm going to do is to go ahead and fix this is saying that this is going to inherit from our BAS model class and I think if we do that then we'll have solved that issue so if you go back to rest forx you see that our token is invalid therefore you need to get a new token I'll go to O and then I'll go to where I get a token pair try to log in with a currently logged in user and I'll get a token when I get this token I'm simply going to go to where I'm making this review or where I'm getting old books and then I have to provide that specific book or that specific token that is Created from our login now this is going to return an empty list of books so let me try to create a new book so when I create a book uh s we having the same issue so I'll go ahead and simply provide a new token and then we have we have a book right here so if if I get this book's ID and then if I get this book's ID and let's say try to add a review to this book so just going to go ahead and remove some of the things we have here so here we are going to provide the book Q ID and this is not found oh this API this is supposed to be on slash API SL version one and then SL reviews so when I sent we are not authenticated therefore have to provide our authorization header come back right here and copy this newly created token I'll provide it right inside where we are creating a review for a book and then we see that our body is missing so let's go ahead and provide it I'll say body this is going to be a Json and for Json or we need to do is to provide the rating so let's say the rating is going to be three out of five and then we are also going to have the review text uh the review text let's say the user is going to provide a review text and this review text is going to be something like nice one or just uh comment we say that we have in this internal server error so I'm going to go back this side and see where that is coming from so this got multiple values for user email let's see where that is coming coming from if you go back to our routes we see that we have user email which is this and so let me try to see where this is so if we go back to our class right here we actually forget to provide self so if you go ahead and provide self and save if we try to go ahead and make this request once again then you see that some something has gone wrong so that something has gone wrong where we adding our books so let's see what exactly is taking place so basically what we having here is internal server error and one thing we can do to try to know what exact error may happen is by using the loging module so I can just come right here and say import loging and once I'm done with that I can log the Exception by using logging do exception and this is just one way you can basically get to log an exception and also understand where it originated from so if you go back right and make the request shall see that something is wrong but when you go to our teral we shall see what exactly went wrong so what went wrong is uh data error and here we have invalid input for query argument one which is that so this is an invalid uu ID therefore we have to go ahead and provide the real uu ID so what I'm going to do is to go back where I go to our books and I will try to see if I've copied the entire U ID which I didn't if I copy this and try to add the review to the book so let me make this a little bit smaller so that you guys can see if I go ahead and provide this U ID right here and send we now see that our view has been created successfully now let's go ahead and also make sure that each book is returned with its reviews so the way we're going to do that is to modify the book schema so we're going to head over to the schemas of books and within our book schemas we are going to make sure that each book is returned with all the reviews that are or each book detail is returned with all the reviews that it contains so we have this model or this class that we created for returning information about a book but what if we wanted to return the detail of a book so what I'm me to do is to create another class similar to what we did for the user class or user schema so for our case we shall have this being the book detail or let's just say this going to be our book detail model and this book detail model will have something like let's say uh it's going to to be a base class of our book class and then it will have a list of reviews that a book has so for our case this is going to be a list so we shall import the typing class so the typing class is going to be the list class so shall say from typing import list and once we've been able to do that we going to say that this is going to be a list of our book of our reviews so we have to go ahead and also import our reviews so so I'm going to come and say from source. reviews dot schemas we are going to go ahead and import our review so we have the review model we also have the review create model if you go ahead and look at this this is going to just simply return information about the review so we're going to come and provide that so we have this and this is going to be a list of our review model so let us try to the book or the detail of the book so for us to see this reflect we have to modify uh we have to modify the detail or the route that is responsible for returning a book so when you go within our books right here we want to get that that Returns the book ID or the book with a certain new ID so we can just simply fix the response model to be that one that Returns the book detail instead of returning just the book so to do that shall just come right right here and say from source. books. schemers or from schemers because we in the same package we can go ahead and also import the book detail schema or the book detail model and once that is done then I'll just come right here and specify that our response model is going to be our book detail model so once that is done I'm now going to go ahead and try to get the detail of the book so that we see our so when you go back to our books uh we're going to try to get the book with this U ID the one that we provided right here if you go to get book by ID so this book does not exist because I deleted most of the books that we had in our database now I'm going to get this token the one that I have right here and provide it where we get the book by U ID and once that is done I'm going to try to get that book which is not found now since that book is not found we now going to provide a book that exists within our database so to get our book which is the book with this ID that we have right here this time we expect that it's going to return all the reviews that are going to be present on that book as well as the book ID and the book U ID so I hope this makes sense we can also do the same thing for our users So within our users we can simply go to our users so for our case we can go back to our users schem as so let's go ahead and try to do it so I'll say reviews and now we shall have a list of reviews so I'll try to do this so I'm going to do this by first of all importing the the schemas for for our reviews so I'm going to do that by going through the top right here and I'm going to say from Source dot this is going to be our reviews do schemers we are going to go ahead and import the books the review scheme or what we call the review model so right here we shall just simply come and say review model so this will be our list of reviews if we try to get the current logged in user I'll copy our token right here we shall now notice that this is going to return details for the current loged in user so we shall have both their books as well as their reviews so this is like the detail of of the user that's going to return all the information about how many reviews they have done on books as well as how many books they have submitted so this is just basically what we have done in this video we've basically built on top of the relationships we set up in the previous video we've also looked at different ways to handle different errors that you may experience inside our relationships if you've enjoyed this video please leave a like to the video do not forget to subscribe and I'll see you in the next video bye
Info
Channel: Ssali Jonathan
Views: 268
Rating: undefined out of 5
Keywords: fastapi, fastapi schemas, fastapi pydantic, fastapi sqlmodel
Id: HFsvl515eKM
Channel Id: undefined
Length: 54min 38sec (3278 seconds)
Published: Fri Jun 28 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.