Heroku:Rails 5部署 - 迁移db期间出错

时间:2016-07-08 19:14:11

标签: postgresql sqlite heroku rails-activerecord ruby-on-rails-5

所以我想将我的应用程序部署到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

的Gemfile:

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。

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

1 个答案:

答案 0 :(得分:1)

问题在于您错误地调用了add_column,但SQLite让您可以逃脱它。来自fine manual

  

add_column (table_name,column_name,type,options = {})
  将名为type的新column_name列添加到table_name   [...]

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

如果使用正确的类型:

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

然后你会有更好的运气。

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