0

私の Rails アプリケーションには、id、description、user_id、created_at、updated_at という列を持つ「Posts」テーブルがあります。

ローカルで、次のクエリを実行しました。

ALTER TABLE posts RENAME TO posts_old;

次に、Rails コンソールで次のコマンドを実行しました。

require Rails.root + 'db/migrate/20130425060156_create_posts.rb'
CreatePosts.new.change

次に、クエリを実行しました。

INSERT INTO posts (id, user_id, content, created_at, updated_at)
SELECT id, user_id, content, created_at, updated_at FROM posts_old;

クエリは正常に実行されましたが、新しい "Posts" テーブルに "user_id" 列がないことに気付きました。

そこで、移行を実行し、新しい「Posts」テーブルの「user_id」列を作成しました。

これらの変更を git にプッシュしました。

最後に、これらの変更を heroku にプッシュしました。最後の移行のために:私は走った

heroku run rake db:migrate

これは、エラーが発生したときです。下記参照。私の質問は、heroku のデータベースに既存のものと競合することなく、新しい「Posts」テーブルに列を追加するにはどうすればよいですか?

Migrating to AddUserIdToPost (20131008050154)
==  AddUserIdToPost: migrating ================================================
-- add_column(:posts, :user_id, :integer)
rake aborted!
An error has occurred, this and all later migrations canceled:

PG::DuplicateColumn: ERROR:  column "user_id" of relation "posts" already exists
: ALTER TABLE "posts" ADD COLUMN "user_id" integer/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:650:in `exec'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:650:in `block in execute'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:649:in `execute'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:1022:in `add_column'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:466:in `block in method_missing'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:438:in     `block in say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:438:in `say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:458:in `method_missing'
/app/db/migrate/20131008050154_add_user_id_to_post.rb:3:in `change'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:407:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:407:in `block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:389:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:528:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:775:in `call'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:775:in `block in ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/transactions.rb:208:in `transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:775:in `ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:719:in `block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:700:in `each'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:700:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:570:in `up'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/migration.rb:551:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/railties/databases.rake:193:in `block (2 levels) in <top (required)>'
4

2 に答える 2

0

移行 CreatePosts の後に、移行 AddUserIdToPost が実行されると思われます。また、移行 CreatePosts には列 user_id が含まれています。移行 CreatePosts から「user_id」を削除します。属性「user_id」は、移行 AddUserIdToPost で作成されます。

于 2013-10-08T06:44:37.220 に答える