Database Schemas

Did you notice we also got a new file in our db directory called schema.rb. This is a running log of the structure of our database or at least as sinatra sees it. It’s updates every time a new migration is applied to our database. And it’s a really useful resource for seeing where things are at with our SQL database.

What is the schema

It’s the authoritative source for your database schema. Active Record generates db/schema.rb by examining the database to represent it’s current state.

Schema files are useful if you want a quick look at what attributes an Active Record object has. This information is not in the model’s code and is frequently spread across several migrations, but the information is nicely summed up in the schema file. The annotate_models gem automatically adds and updates comments at the top of each model summarizing the schema if you desire that functionality.

What does it look like

A schema looks something like this:

ActiveRecord::Schema.define(version: 20161101024543) do

  create_table "lists", force: :cascade do |t|
    t.string "name"

  create_table "tasks", force: :cascade do |t|
    t.string  "name"
    t.integer "list_id"
    t.boolean "is_complete", default: false


You’ll notice that:

  • The version will match a number from one of your migrations. This tells ActiveRecord if there are migrations that haven’t been applied. And this is also why there can’t be two migrations with the same ID number.

  • Each of the tables are listed and we can see what columns exist for them.

Find out more



The latest tutorials sent straight to your inbox.