Search code examples
ruby-on-railsrubysqliteomniauthomniauth-twitter

Error on migration: SQLite3::SQLException: no such table: main.users


I'm still pretty new to the Rails world. I've been working on a skill at a time so I thought I'd give Omni-Auth twitter a crack. I've been reading through this tutorial on SitePoint:

Rails Authentication with OAuth 2.0 and OmniAuth

I'm find up until the point where it has me create a user model and modify the migration file before running rake db:migrate. Here's my migration file based on those instructions:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :provider, null: false
      t.string :uid, null: false
      t.string :name
      t.string :location
      t.string :image_url
      t.string :url
      add_index :users, :providers
      add_index :users, :uid
      add_index :users, [:provider, :uid], unique: true

      t.timestamps null: false
    end
  end
end

But when I run rake db:migrate it throws this error:

SQLite3::SQLException: no such table: main.users: CREATE  INDEX "index_users_on_providers" ON "users" ("providers")/Users/jrshafer/.rvm/gems/ruby-2.2.1/gems/sqlite3-1.3.11/lib/sqlite3/database.rb:91:in `initialize'

Any help you could provide this aspiring developer would be much appreciated.

You can view the full repo here: kronoTweeter GitHub Repo


Solution

  • You should create your table in one block and then add the indexes. But in your current code, you are trying to add the indexes in the same create_table block.

    Try replacing your current code with this piece of code:

    class CreateUsers < ActiveRecord::Migration
      def change
        create_table :users do |t|
          t.string :provider, null: false
          t.string :uid, null: false
          t.string :name
          t.string :location
          t.string :image_url
          t.string :url
          t.timestamps null: false
        end # you have to end the create_table block here
    
        # then you have to have the add_index statements
        add_index :users, :providers
        add_index :users, :uid
        add_index :users, [:provider, :uid], unique: true
      end
    end
    

    Then run:

    bundle exec rake db:migrate
    

    This should fix your problem.