Schema Integration with Database
Understand how to adapt Ecto schemas to correspond with database tables by setting primary keys, timestamps, and unique constraints. Explore updating schema modules and test files to ensure integration with the database and passing test cases.
We'll cover the following...
We’re now ready to update our schema to match this table.
Updating our schema for database
Technically we’ll update a copy to the schema to make changes without breaking our other test files.
Let’s copy testing_ecto/lib/schemas/user_basic_schema.ex to a new file called testing_ecto/lib/schemas/user_database_schema.ex. We’ll copy the test file to match later. For now, we’ve updated the schema file to look like this:
Note: The changes are consolidated for you in the below-given widget. Simply replace the contents in the specified file.
Let’s look at what changes we have made to our code testing_ecto/lib/schemas/user_basic_schema to create testing_ecto/lib/schemas/user_database_schema.ex as above:
-
First, we changed the module name in
user_database_schema.exto beTestingEcto.Schemas.UserDatabaseSchema, like on line 4. -
We need to set some Ecto Schema options in the file (using module attributes) to make it compatible with the timestamp and primary key ...