1 00:00:00,650 --> 00:00:04,430 Let's continue by adding a constraint for our user model. 2 00:00:04,430 --> 00:00:06,100 By constraints, I mean rules for 3 00:00:06,100 --> 00:00:10,830 more in-depth checks performed at the SQL level versus at the sequelize level. 4 00:00:10,830 --> 00:00:14,896 For example, earlier we use the notEmpty validator to ensure that 5 00:00:14,896 --> 00:00:17,590 values don't get stored as empty strings. 6 00:00:17,590 --> 00:00:21,630 And the isEmail validator to check the email format. 7 00:00:21,630 --> 00:00:24,590 As you learned, if any of those validations fail, 8 00:00:24,590 --> 00:00:28,090 a SQL query will not be sent to the database. 9 00:00:28,090 --> 00:00:32,130 However, with constraint checks a SQL query is performed. 10 00:00:32,130 --> 00:00:35,150 But if the constraint check fails, the database throws an error 11 00:00:35,150 --> 00:00:39,310 preventing the post or update, and sequelize lets us know about it. 12 00:00:39,310 --> 00:00:42,790 This is how constraint checks differ from standard validation. 13 00:00:42,790 --> 00:00:48,040 A common example is checking that the email submitted is a unique email address. 14 00:00:48,040 --> 00:00:51,916 A user submits an entry and their email gets checked against other emails 15 00:00:51,916 --> 00:00:54,338 in the database to ensure that it's unique. 16 00:00:54,338 --> 00:00:59,445 In the user model, I can define a unique constraint on the email field 17 00:00:59,445 --> 00:01:04,830 by adding the unique property to the email object and setting it to true. 18 00:01:06,550 --> 00:01:11,870 Back in Postman, I'll attempt to create a new user entry using an email that 19 00:01:11,870 --> 00:01:18,330 already exists in the users table test@teamtreehouse.com. 20 00:01:18,330 --> 00:01:21,970 When I press Send, I get a 400 status and 21 00:01:21,970 --> 00:01:25,920 see the error email must be unique in the response. 22 00:01:26,920 --> 00:01:32,896 And notice in the console that the error log is SequelizeUniqueConstrainError. 23 00:01:32,896 --> 00:01:36,521 An attempt to insert an email that already exists will throw 24 00:01:36,521 --> 00:01:38,770 a SequeleUniqueConstraintError, 25 00:01:38,770 --> 00:01:43,130 which prevents duplicate data from being inserted into the database. 26 00:01:44,740 --> 00:01:49,470 Like validators, we can customize the error message for unique constraints. 27 00:01:49,470 --> 00:01:52,900 Set the value of unique to an object, 28 00:01:52,900 --> 00:01:57,890 inside the object use the message property and set it to your custom message. 29 00:01:57,890 --> 00:02:01,065 For example, the email you entered already exists. 30 00:02:07,063 --> 00:02:11,922 I'll test sending the same post request, and this time I receive my 31 00:02:11,922 --> 00:02:16,881 custom error message in the response. Changing the email address 32 00:02:19,597 --> 00:02:24,382 successfully creates and stores the new user account in the database. 33 00:02:24,382 --> 00:02:28,890 All right, I set up my one constraint for unique user email addresses. 34 00:02:28,890 --> 00:02:32,210 Next, I'll continue by adding two more user model validations.