0

对于我的 rails 应用程序,我有一个“帖子”表,其中包含以下列:id、description、user_id、created_at、updated_at。

在本地,我运行了以下查询:

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;

查询运行良好,但我意识到的问题是我的新“帖子”表中缺少“user_id”列。

所以我进行了迁移并为新的“帖子”表创建了一个“user_id”列。

我将这些更改推送到 git。

最后,我将这些更改推送到 heroku。由于上次迁移:我跑了

heroku run rake db:migrate

这是我遇到错误的时候。见下文。我的问题是,如何在我的新“帖子”表中添加一列而不与 heroku 数据库中现有的内容冲突?

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

我怀疑迁移 AddUserIdToPost 是在迁移 CreatePosts 之后执行的。迁移 CreatePosts 包含 user_id 列。从迁移 CreatePosts 中删除“user_id”。属性“user_id”将在迁移 AddUserIdToPost 中创建。

于 2013-10-08T06:44:37.220 回答
0

更新了 AddUserIdToPost 从

def change
    add_column :posts, :user_id, :integer
end

对此:

def up
    add_column :posts, :user_id, :integer
end

def down
end

然后运行以下内容:

bundle exec rake db:rollback STEP=1

bundle exec rake db:migrate

然后向上推到 git、heroku 并运行 heroku 迁移。

于 2013-10-12T18:54:10.410 回答