0
我遇到了One Month Rails應用程序的問題。不能耙分貝:遷移
我已經運行
rails generate migration add_user_id_to_pins user_id:integer:index但是,當我嘗試直接運行
rake db:migrate後,我產生的遷移,我得到這個錯誤:
== AddUserIdToPins: migrating ================================================
-- add_column(:pins, :user_id, :integer)
rake aborted!
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: duplicate column name: user_id: ALTER TABLE "pins" ADD "user_id" integer/home/colin/.rvm/gems/ruby-2.0.0-p451/gems/sqlite3- 1.3.9/lib/sqlite3/database.rb:91:in `initialize'
/home/colin/.rvm/gems/ruby-2.0.0-p451/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:91:in `new'
/home/colin/.rvm/gems/ruby-2.0.0-p451/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:91:in `prepare'
/home/colin/.rvm/gems/ruby-2.0.0-p451/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:134:in `execute'
/home/colin/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.3/lib/active_record/connection_adapters/sqlite3_adapter.rb:331:in `block in execute'
/home/colin/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.3/lib/active_record/connection_adapters/abstract_adapter.rb:435:in `block in log'
/home/colin/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.3/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/colin/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.3/lib/active_record/connection_adapters/abstract_adapter.rb:430:in `log'
/home/colin/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.3/lib/active_record/connection_adapters/sqlite3_adapter.rb:331:in `execute'
/home/colin/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:360:in `add_column'
/home/colin/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.3/lib/active_record/connection_adapters/sqlite3_adapter.rb:454:in `add_column'
/home/colin/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.0.3/lib/active_record/migration.rb:625:in `block in method_missing'
這些都是我在遷移中產生的文件的內容:
class AddUserIdToPins < ActiveRecord::Migration
def change
create_table :pins do |t|
t.integer :user_id
t.timestamps
end
add_index :pins, :user_id
end
def change
Pins.reset_column_information
add_column :pins, :user_id, :integer
add_index :pins, :user_id
end
end
我認爲這可能是因爲我有多次嘗試生成遷移,但我刪除了以前的遷移,所以我不確定是什麼導致了這種情況。
感謝您的回答。我認爲你解決了我的問題,但它不幸造成了另一個問題。現在我收到錯誤:「SQLite3 :: SQLException:table」pins「已存在:CREATE TABLE」pins「(」id「INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,」user_id「integer,」created_at「datetime,」updated_at「datetime) 「當嘗試db:migrate時。 – user3172050
首先,您需要'rake db:rollback version = version_number',其中'20140125191228'替換version_number,如果遷移文件是'20140125191228_create_pins.rb' –
運行後'rake db:migrate'。 –