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:
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.
Bee migrations: Beego provides a feature via the
bee
command line to create database migrations. These migrations are part of the code. Thebee
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:
Create a new migration file using the
bee
command line.Add SQL statements to the migration file for migration.
Run the migration using the
bee
command line.
Get hands-on with 1400+ tech skills courses.