簡體   English   中英

Rails 4 SQLite3 :: SQLException錯誤

[英]Rails 4 SQLite3::SQLException error

我有一個設計用戶模型,我想添加一個管理布爾字段,所以我跑了

rails生成遷移add_admin_to_users admin:boolean ,它創建了以下遷移

class AddAdminToUsers < ActiveRecord::Migration
 def change
  add_column :users, :admin, :boolean
 end
end

但是,當我運行rake db:migrate時,我不斷收到以下錯誤

SQLite3 :: SQLException:沒有這樣的表:users:ALTER TABLE“users”ADD“admin”boolean / home / notebook / .rvm / gems / ruby​​-2.0.0-p353 / gems / sqlite3-1.3.8 / lib / sqlite3 /database.rb:91:in`initialize'

我試圖rake db:migrate VERSION = 0回滾到開頭並重做rake db:migrate但我一直得到同樣的錯誤

這是我的用戶模型遷移文件

class DeviseCreateUsers < ActiveRecord::Migration
  def change
   create_table(:users) do |t|
  ## Database authenticatable
  t.string :email,              :null => false, :default => ""
  t.string :encrypted_password, :null => false, :default => ""

  ## Recoverable
  t.string   :reset_password_token
  t.datetime :reset_password_sent_at

  ## Rememberable
  t.datetime :remember_created_at

  ## Trackable
  t.integer  :sign_in_count, :default => 0, :null => false
  t.datetime :current_sign_in_at
  t.datetime :last_sign_in_at
  t.string   :current_sign_in_ip
  t.string   :last_sign_in_ip

  ## Confirmable
  t.string   :confirmation_token
  t.datetime :confirmed_at
  t.datetime :confirmation_sent_at
  t.string   :unconfirmed_email # Only if using reconfirmable

  ## Lockable
  # t.integer  :failed_attempts, :default => 0, :null => false # Only if lock strategy is :failed_attempts
  # t.string   :unlock_token # Only if unlock strategy is :email or :both
  # t.datetime :locked_at
  t.timestamps
end

add_index :users, :email,                :unique => true
add_index :users, :reset_password_token, :unique => true
add_index :users, :confirmation_token,   :unique => true
# add_index :users, :unlock_token,         :unique => true
end
end

我的db / schema.rb文件如下:

ActiveRecord::Schema.define(version: 20140217093954) do
  create_table "entities", force: true do |t|
   t.string   "entity"
   t.string   "genre"
   t.string   "url"
   t.datetime "created_at"
   t.datetime "updated_at"
end

   add_index "entities", ["entity"], name: "index_entities_on_entity", unique: true

結束

聽起來您的開發數據庫與架構不同步(或者您認為架構是什么)。 運行數據庫遷移旨在用於增量更改,而不是用於回滾數據庫並從第一次遷移運行的方法。 為此,您希望使用rake db:reset ,它將刪除數據庫並從db / schema加載模式。 這篇文章很好地概述了不同的數據庫rake命令。

您的數據庫沒有users表,您需要先創建users表:

rails g migration create_users

並將該遷移放在您嘗試運行的遷移之前,即更改其時間戳。

更簡單的選擇是添加當前文件:

class AddAdminToUsers < ActiveRecord::Migration
 def change
  create_table :users
  add_column :users, :admin, :boolean
 end
end

無論您應用哪種解決方案,都會遇到遷移順序問題,在遷移文件夾中搜索實際創建users表的遷移。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM