Create a Feature in a Database Using SQLite3 and Active Record: Creating a Table

Sanna Sakinah Ishaq
4 min readApr 15, 2022

Coming from frontend development and being introduced to backend is analogous to being thrown into an ocean without knowing how to swim. It’s dark and overwhelming, and it can feel like you’re never going to see the light. After reading through endless documentation, and watching YouTube videos, you come to realize that it’s not too bad.

Yeah, it seems strange that you can’t immediately see your results on the browser as you do when you’re working with JavaScript and HTML, but the results you receive doing backend are quite rewarding. Backend is what “saves” the data that is added in using the front-end. Without backend, there is no data persistence!

For most developers, one of the most difficult concepts to wrap your head around is how to create database migrations. Let’s start off with the basic question of what are migrations? Migrations are just a convenient way to change your database schema over time. Each migration is like a “new version” of the database. With that being said, let’s dive into creating a table using ActiveRecord and SQLite3!

With your ruby environment already created and bundle install completed, let’s begin executing the following:

  1. run rake db:create_migration NAME=description_of_table in your terminal (make sure you’re in the correct directory)
  • By convention, table names must be lowercase, snake-cased, and pluralized.
  • This command generates a migration file: a migration file is like a “template” of how the table (database) is going to look for the model
  • Once you’ve run this command, you will have a new file automatically created in your migrate folder; this file will contain a class that resembles your table name from the terminal command as well as a method called change
This is an image depicting the end result of running the rake db:create_migration NAME=file_name
After running the db:create_migration command in the terminal, the migrate folder on the left side-bar has a new file that contains numbers (time stamp of when you created the file) and the name of the file you created

2. Write code in the migration file to indicate what you want to see in your table (be sure to specify the datatype)

Inside of the autogenerated change method, use the create_table method to add your attributes to the table

Note:

- :artists indicates the table name

- t is the conventional parameter/iterator we use to assign the data type with its associated attribute

- each starting and ending of a “block” is indicated by the light-grey vertical line (make sure you end all of your blocks: class/end , def/end , do/end)

3. run the migration with the data you created in the migration file by using the rake db:migrate command into the terminal

The left side-bar now contains a development.sqlite3 file as well as a schema file that contains the schema of your database

-this command will automatically create a development folder that will give us access to the SQLite3 extension and as well as an auto-generated schema.rb file

4. If you want to check the status of your migration use rake db:migrate:status in the terminal;

  • if the status is up: the migration is active and the database is updated
  • if the status is down: the migration is no longer part of the database

5. To add data to your SQLite3 table, add new data into your seed file using the .create method

  • Now run rake db:seed in your terminal to “plant” the seed data into the database

6. Open up your SQLite3 extension in your IDE (for mac users on VS Code: ctrl+ shift+ p will open up the search bar in the IDE)

  • click on the database that matches the one you are writing your code in and an SQLite3 window should be generated on bottom of the left side-bar
  • click on the SQLite3 window and open the tab with the table name “artists” and click the play button on the right of the table name
This is the SQLite generated table using the seeds you created!

You did it!

You created your first migration file and executed your “seeds” flawlessly!

This is no small feat, so congratulations programmer :)

--

--