Improving unique constraints in the models
When we coded the NotificationCategory
model in the previous chapter, we specified the True
value for the unique
argument in the creation of the orm.Column
instance named name
. As a result, the migrations process generated the necessary unique constraint to make sure that the name
field has unique values in the notification_category
table. This way, the PostgreSQL database won't allow us to insert duplicate values for the notification_category.name
column. However, the error message generated when we try to do so is not clear. The message includes details about the database structure that shouldn't be mentioned in the error message.
Run the following command to create a category with a duplicate name. There is already an existing category with the name equal to 'Warning'
. The code file for the sample is included in the restful_python_2_03_01
folder, in the Flask01/cmd301.txt
file:
http POST ":5000/service/notification_categories/" name='Warning'
The...