【问题标题】:Error on migration: SQLite3::SQLException: no such table: main.users迁移错误:SQLite3::SQLException:没有这样的表:main.users
【发布时间】:2015-10-15 03:20:02
【问题描述】:

我对 Rails 世界还是很陌生。我一直在研究一项技能,所以我想我会给 Omni-Auth 推特一个破解。我一直在阅读 SitePoint 上的本教程:

Rails Authentication with OAuth 2.0 and OmniAuth

直到它让我创建一个用户模型并在运行rake db:migrate 之前修改迁移文件为止。这是我基于这些说明的迁移文件:

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

但是当我运行rake db:migrate 时,它会抛出这个错误:

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'

您可以为这位有抱负的开发人员提供的任何帮助将不胜感激。

您可以在此处查看完整的 repo: kronoTweeter GitHub Repo

【问题讨论】:

    标签: ruby-on-rails ruby sqlite omniauth omniauth-twitter


    【解决方案1】:

    您应该在一个块中创建表,然后添加索引。但在您当前的代码中,您正尝试在同一 create_table 块中添加索引。

    尝试用这段代码替换您当前的代码:

    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
    

    然后运行:

    bundle exec rake db:migrate
    

    这应该可以解决您的问题。

    【讨论】:

    • 这应该是答案
    猜你喜欢
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 2022-08-15
    • 2016-11-09
    相关资源
    最近更新 更多