First Model: Note Model

Learn the significance of a model to interact with the database. Also, learn to create and run database migrations.

Significance of models

In the notes application (or any web application), a critical component is the model, which defines the structure of the database table used to store notes. A model serves as a blueprint, specifying the attributes and properties of each entry within the table. By creating a model for the notes table, we establish a structured framework for organizing and managing data efficiently.

Database migrations

Let’s understand how database migrations can be handled in the Beego framework. To keep track of migrations, there are two ways used by developers:

  1. SQL commands: Developers track all the SQL commands they run to create, delete, or alter tables in the database. The primary disadvantage of this approach is that it is not easy to keep track of changes in different environments.

  2. Bee migrations: Beego provides a feature via the bee command line to create database migrations. These migrations are part of the code. The bee command saves migrations that have run already in the database. When we run the migration command, it runs only the ones that have never been run before, therefore making the whole process easy.

Migration through bee

Here are the steps involved in database migration using the bee command line:

  1. Create a new migration file using the bee command line.

  2. Add SQL statements to the migration file for migration.

  3. Run the migration using the bee command line.

Get hands-on with 1400+ tech skills courses.