如何解决本地vs生产(heroku)中不一致的数据库表列?

时间:2013-10-08 05:55:52

标签: sql ruby-on-rails git heroku

对于我的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)>'

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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

     

捆绑exec rake db:migrate

然后推送到git,heroku,并运行heroku迁移。