Heroku:Rails 5 部署 - 迁移数据库时出错

Heroku: Rails 5 deployment - error during migrating db

所以我想将我的应用程序部署到 heroku,但它不支持 sqlite3,所以我将生产设置为使用 postgres。我认为问题是为 sqlite 生成的一些迁移与 pg 不兼容。

'heroku run rake db:migrate'的结果:

Running rake db:migrate on ⬢ ground-tree... up, run.6485
    D, [2016-07-08T19:02:25.697588 #3] DEBUG -- :    (0.8ms)  SELECT pg_try_advisory_lock(1215818661303848385);
D, [2016-07-08T19:02:25.715589 #3] DEBUG -- :   ActiveRecord::SchemaMigration Load (1.0ms)  SELECT "schema_migrations".* FROM "schema_migrations"
I, [2016-07-08T19:02:25.724349 #3]  INFO -- : Migrating to AddWinnerToItems (20160706115655)
D, [2016-07-08T19:02:25.726558 #3] DEBUG -- :    (0.8ms)  BEGIN
== 20160706115655 AddWinnerToItems: migrating =================================
-- add_column(:items, :winner_id, :references, {:foreign_key=>true})
D, [2016-07-08T19:02:25.728658 #3] DEBUG -- :    (1.5ms)  ALTER TABLE "items" ADD "winner_id" references
D, [2016-07-08T19:02:25.729640 #3] DEBUG -- :    (0.7ms)  ROLLBACK
D, [2016-07-08T19:02:25.730786 #3] DEBUG -- :    (0.8ms)  SELECT pg_advisory_unlock(1215818661303848385)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::SyntaxError: ERROR:  syntax error at or near "references"
LINE 1: ALTER TABLE "items" ADD "winner_id" references
                                            ^
: ALTER TABLE "items" ADD "winner_id" references
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `async_exec'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `block in execute'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:566:in `block in log'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:560:in `log'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:97:in `execute'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:547:in `add_column'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/schema_statements.rb:471:in `add_column'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:845:in `block in method_missing'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:814:in `block in say_with_time'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:814:in `say_with_time'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:834:in `method_missing'
/app/db/migrate/20160706115655_add_winner_to_items.rb:3:in `change'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:788:in `exec_migration'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:772:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:771:in `block in migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:398:in `with_connection'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:770:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:950:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1211:in `block in execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1279:in `block in ddl_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/transactions.rb:211:in `transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1279:in `ddl_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1210:in `execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1183:in `block in migrate_without_lock'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1182:in `each'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1182:in `migrate_without_lock'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1131:in `block in migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1298:in `with_advisory_lock'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1131:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1005:in `up'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:983:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/tasks/database_tasks.rb:161:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/railties/databases.rake:58:in `block (2 levels) in <top (required)>'
/app/vendor/bundle/ruby/2.3.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR:  syntax error at or near "references"
LINE 1: ALTER TABLE "items" ADD "winner_id" references
                                            ^
: ALTER TABLE "items" ADD "winner_id" references
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `async_exec'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `block in execute'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:566:in `block in log'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:560:in `log'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:97:in `execute'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:547:in `add_column'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/schema_statements.rb:471:in `add_column'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:845:in `block in method_missing'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:814:in `block in say_with_time'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:814:in `say_with_time'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:834:in `method_missing'
/app/db/migrate/20160706115655_add_winner_to_items.rb:3:in `change'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:788:in `exec_migration'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:772:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:771:in `block in migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:398:in `with_connection'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:770:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:950:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1211:in `block in execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1279:in `block in ddl_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/transactions.rb:211:in `transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1279:in `ddl_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1210:in `execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1183:in `block in migrate_without_lock'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1182:in `each'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1182:in `migrate_without_lock'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1131:in `block in migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1298:in `with_advisory_lock'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1131:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1005:in `up'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:983:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/tasks/database_tasks.rb:161:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/railties/databases.rake:58:in `block (2 levels) in <top (required)>'
/app/vendor/bundle/ruby/2.3.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
PG::SyntaxError: ERROR:  syntax error at or near "references"
LINE 1: ALTER TABLE "items" ADD "winner_id" references
                                            ^
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `async_exec'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `block in execute'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:566:in `block in log'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:560:in `log'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:97:in `execute'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:547:in `add_column'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/schema_statements.rb:471:in `add_column'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:845:in `block in method_missing'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:814:in `block in say_with_time'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:814:in `say_with_time'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:834:in `method_missing'
/app/db/migrate/20160706115655_add_winner_to_items.rb:3:in `change'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:788:in `exec_migration'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:772:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:771:in `block in migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:398:in `with_connection'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:770:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:950:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1211:in `block in execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1279:in `block in ddl_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/transactions.rb:211:in `transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1279:in `ddl_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1210:in `execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1183:in `block in migrate_without_lock'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1182:in `each'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1182:in `migrate_without_lock'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1131:in `block in migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1298:in `with_advisory_lock'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1131:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1005:in `up'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:983:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/tasks/database_tasks.rb:161:in `migrate'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/railties/databases.rake:58:in `block (2 levels) in <top (required)>'
/app/vendor/bundle/ruby/2.3.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

似乎会导致问题的迁移:

class AddWinnerToItems < ActiveRecord::Migration[5.0]
  def change
    add_column :items, :winner_id, :references, foreign_key: true
  end
end

宝石文件:

source 'https://rubygems.org'

ruby '2.3.0'

gem 'rails', '~> 5.0.0'
gem 'pg'
gem 'puma', '~> 3.0'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'
gem 'jquery-rails'
gem 'jbuilder', '~> 2.5'
gem 'slim-rails'
gem 'bootstrap-sass'
gem 'devise'
gem 'carrierwave'
gem 'material_design_lite-sass'
gem 'ransack'
gem 'kaminari'
gem 'rolify'
gem 'business_process'
gem 'decent_exposure', '3.0.0'
gem 'friendly_id', '~> 5.1.0'
gem 'simple_form'
gem 'draper', github: 'audionerd/draper', branch: 'rails5'
gem 'activemodel-serializers-xml'
gem 'decent_decoration'

group :development, :test do
  gem 'pry-rails'
  gem 'dotenv-rails'
  gem 'ffaker'
  gem 'better_errors'
end

group :development do
  gem 'binding_of_caller'
  gem 'sqlite3'
  gem 'letter_opener'
  gem 'web-console'
  gem 'listen', '~> 3.0.5'
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

相关'database.yml'部分:

default: &default
  pool: 5
  timeout: 5000

production:
  <<: *default
  database: ground_tree
  adapter: postgresql

这里是link to app on github

:belongs_to 类型有问题(我假设 pg 不支持这个名称),所以我将所有 :belongs_to 更改为 :references.

现在我收到这个错误,我不知道如何解决这个问题。

问题是您调用 add_column 不正确,但 SQLite 让您得以逃脱。来自 fine manual:

add_column(table_name, column_name, type, options = {})
Add a new type column named column_name to table_name.
[...]

add_column 的第三个参数应该是列类型,但您传递的是 :reference。 PostgreSQL 中没有 reference 类型,所以它会抱怨。 SQLite 中也没有 reference 类型,但 SQLite 认为任何它不知道的类型都是 text 的别名,所以它默默地让你摆脱它。

如果你使用正确的类型:

add_column :items, :winner_id, :integer, foreign_key: true
# -----------------------------^^^^^^^^

那么你的运气会更好

有关详细信息,请参阅 Active Record Migrations 指南。