Saggex
Saggex

Reputation: 3500

how to add table to sqlite3 database

How can I add a database to my SQLite3 database? I always get this error.

ActiveRecord::StatementInvalid in Timespans#index

Showing C:/xampp/htdocs/fluxcapacitor/app/views/timespans/index.html.erb where line #19 raised:

SQLite3::SQLException: no such table: timespans: SELECT "timespans".* FROM "timespans"

I tried adding my table to myschema.rb, but it doesn't work. It is always overwritten, when I run

rake db:migrate RAILS_ENV=development

Upvotes: 0

Views: 210

Answers (2)

Jakob S
Jakob S

Reputation: 20125

You want to use migrations to make changes to your database. See the Active Record Migrations guide for the details.

Generally speaking, the process is something like:

  1. rails generate migration create_timespans generates an empty migration for you.
  2. Edit the created migration to setup the table like you want it to. This should be pretty similar to what you've already added in schema.rb.
  3. rake db:migrate then applies those changes to the database.

Upvotes: 2

Marek Lipka
Marek Lipka

Reputation: 51151

You should generate migration:

bundle exec rails g migration create_timespans

And in the migration, you should have:

class CreateTimespans < ActiveRecord::Migration
  def change
    create_table :timespans
      t.string :column1
      t.string :column2
      # ...
    end
  end
end

And run it with bundle exec rake db:migrate.

This is the proper way to make changes in your DB schema in Rails application.

Upvotes: 3

Related Questions