![]() ![]() So, we want to have our Hero model that declares the data in the database: It would be a lot simpler for that code to know that the id from a response is required and will always have a value. That's a lot of unnecessary checks and unnecessary code that could have been saved by declaring the schema properly. Now, what's the matter with having one id field marked as "optional" in a response when in reality it is always required?įor example, automatically generated clients in other languages (or also in Python) would have some declaration that this field id is optional.Īnd then the developers using those clients in their languages would have to be checking all the time in all their code if the id is not None before using it anywhere. ![]() □ So Why is it Important to Have Required IDs ¶ Making both sides very clear will make it much easier to interact with the API.Īnd in most of the cases, the developer of the client for that API will also be yourself, so you are doing your future self a favor by declaring those schemas for requests and responses. The clients could be a frontend application, a command line program, a graphical user interface, a mobile application, another backend application, etc.Īnd the code those clients write depends on what our API tells them they need to send, and what they can expect to receive. The ultimate goal of an API is for some clients to use it. Why Is it Important to Compromise with the Responses ¶ This would mean that our application is making the compromise with the clients that if it sends a hero, it would for sure have an id with a value, it would not be None. So the id in the responses could be declared as required too. This is because in our SQLModel class we declare the id with Optional, because it could be None in memory until we save it in the database and we finally get the actual ID.īut in the responses, we would always send a model from the database, and it would always have an ID. Here's the weird thing, the id currently seems also "optional". The age is optional, we don't have to return it, or it could be None (or null in JSON), but the name and the secret_name are required. This means that our API application is required to return those fields in the response: ![]() The fields with a red asterisk ( *) are "required". If you click the small tab Schema instead of the Example Value, you will see something like this: Now let's review the schema of the response we send back to the client in the docs UI. We want the client only to send the data that is needed to create a new hero:Īnd we want the id to be generated automatically by the database, so we don't want the client to send it. ![]() This means that the client could try to use the same ID that already exists in the database for another hero. If we pay attention, it shows that the client could send an id in the JSON body of the request. Let's start by reviewing the automatically generated schemas from the docs UI. Here you will see the main and biggest feature of SQLModel. We have been using the same Hero model to declare the schema of the data we receive in the API, the table model in the database, and the schema of the data we send back in responses.īut in most of the cases, there are slight differences. Test Applications with FastAPI and SQLModelĪlternatives, Inspiration and Comparisons Read Heroes with Limit and Offset with FastAPIįastAPI Path Operations for Teams - Other Models So Why is it Important to Have Required IDsĬolumns and Inheritance with Multiple Models Why Is it Important to Compromise with the Responses Update and Remove Many-to-Many Relationships Create a Table with SQLModel - Use the EngineĪutomatic IDs, None Defaults, and Refreshing DataĬreate Data with Many-to-Many Relationships ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |