Ruby on rails rails迁移在本地Postgresql上运行,但在prod上失败

Ruby on rails rails迁移在本地Postgresql上运行,但在prod上失败,ruby-on-rails,postgresql,heroku,ruby-on-rails-4,Ruby On Rails,Postgresql,Heroku,Ruby On Rails 4,简言之,在本地Postgres实例上拖放、创建和运行migrate可以多次为我的应用程序创建工作数据库,但在Heroku的prod上使用相同的技术总会产生: heroku run rake db:migrate Running `rake db:migrate` attached to terminal... up, run.9674 PG::UndefinedTable: ERROR: relation "mytable" does not exist LINE 5:

简言之,在本地Postgres实例上拖放、创建和运行migrate可以多次为我的应用程序创建工作数据库,但在Heroku的prod上使用相同的技术总会产生:

heroku run rake db:migrate
Running `rake db:migrate` attached to terminal... up, run.9674
PG::UndefinedTable: ERROR:  relation "mytable" does not exist
LINE 5:                WHERE a.attrelid = '"mytable"'::regclass
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                FROM pg_attribute a LEFT JOIN pg_attrdef d
                  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
               WHERE a.attrelid = '"mytable"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum

rake aborted!
PG::UndefinedTable: ERROR:  relation "mytable" does not exist
LINE 5:                WHERE a.attrelid = '"mytable"'::regclass
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                FROM pg_attribute a LEFT JOIN pg_attrdef d
                  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
               WHERE a.attrelid = '"mytable"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum
编辑:有关迁移期间访问控制器的线索,请参见问题底部

这就是在本地有效的方法:

rake db:drop
rake db:create
rake db:migrate
我有

  • 通过执行
    Heroku pg:psql
    ,然后执行
    \dt
    来验证0个表是否存在,从而在迁移之前确认Heroku的数据库为空
  • 在迁移之前尝试了heroku pg:重置数据库
  • 已确认本地和prod Postgres版本均为9.2.4
  • 尝试重命名
    db/migrate
    中的“mytable”迁移文件,使其具有最早的时间戳,以便首先运行
这是一个相当简单的应用程序,所以它是非常令人沮丧的,一些基本的东西,如从头开始创建一个数据库,不断失败。有什么想法吗

“mytable”迁移:

class CreateMytable < ActiveRecord::Migration
  def change
    create_table :mytable do |t|
      t.string :codes
      t.string :name

      t.timestamps
    end
  end
end
class CreateTable2 < ActiveRecord::Migration
  def change
    create_table :table2 do |t|
      t.references :a, index: true
      t.references :b, index: true
      t.string :c
      t.string :d

      t.timestamps
    end
  end
end
class CreateMytable
可能的参照迁移:

class CreateMytable < ActiveRecord::Migration
  def change
    create_table :mytable do |t|
      t.string :codes
      t.string :name

      t.timestamps
    end
  end
end
class CreateTable2 < ActiveRecord::Migration
  def change
    create_table :table2 do |t|
      t.references :a, index: true
      t.references :b, index: true
      t.string :c
      t.string :d

      t.timestamps
    end
  end
end
class CreateTable2
编辑:在运行db:migrate with--trace时注意到一条线索。下面的堆栈顶部跟踪错误显示控制器错误??为什么控制器要参与迁移

/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:768:in `exec'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:768:in `exec_no_cache'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:138:in `block in exec_query'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:425:in `block in log'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:420:in `log'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:915:in `column_definitions'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/schema_statements.rb:174:in `columns'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/schema_cache.rb:114:in `block in prepare_default_proc'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/schema_cache.rb:56:in `yield'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/schema_cache.rb:56:in `columns'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/model_schema.rb:208:in `columns'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/model_schema.rb:242:in `column_defaults'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/locking/optimistic.rb:169:in `column_defaults'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/core.rb:181:in `initialize'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/inheritance.rb:27:in `new'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/inheritance.rb:27:in `new'
/app/app/controllers/home_controller.rb:7:in `<class:HomeController>'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active\u record/connection\u adapters/postgresql\u adapter.rb:768:in'exec'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active\u record/connection\u adapters/postgresql\u adapter.rb:768:in'exec\u no\u cache'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active\u record/connection\u adapters/postgresql/database\u statements.rb:138:in`block in exec\u query'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active\u record/connection\u adapters/abstract\u adapter.rb:425:in`block in log'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.0/lib/active\u support/notifications/instrumenter.rb:20:in'instrument'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active\u record/connection\u adapters/abstract\u adapter.rb:420:在“日志”中
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active\u record/connection\u adapters/postgresql/database\u statements.rb:137:in'exec\u query'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active\u record/connection\u adapters/postgresql\u adapter.rb:915:在“列定义”中
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active\u record/connection\u adapters/postgresql/schema\u语句。rb:174:in`columns'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active\u record/connection\u adapters/schema\u cache.rb:114:in`block in prepare\u default\u proc'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active\u record/connection\u adapters/schema\u cache.rb:56:in'yield'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active\u record/connection\u adapters/schema\u cache.rb:56:in'columns'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active\u record/model\u schema.rb:208:在“列”中
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active\u record/model\u schema.rb:242:在“column\u defaults”中
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active\u record/locking/optimistic.rb:169:在“column\u defaults”中
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active\u record/core.rb:181:in'initialize'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active\u record/heritation.rb:27:in'new'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active\u record/heritation.rb:27:in'new'
/app/app/controllers/home\u controller.rb:7:in`'

问题中的第7行包含对Mytable.new(…
)的调用。在db:migrate期间控制器代码是如何进入作用域的?

这是由于在引用“Mytable”类的Rails控制器中设置了类变量引起的。因此基本上:

 class HomeController < ApplicationController
  @@data = {MyTable.new(...
class HomeController
将其移动到控制器方法内部并使其成为非类变量修复了问题(我没有确定是类变量方面还是类定义正下方的位置导致的)


我仍然不明白为什么控制器代码是db迁移的一个因素。我很想知道这是否是我做错了什么,或者是rake、rails或其他方面的错误。只发生在Heroku产品中,rails 4.0.0

这是由于在引用“mytable”类。因此基本上:

 class HomeController < ApplicationController
  @@data = {MyTable.new(...
class HomeController
将其移动到控制器方法内部并使其成为非类变量修复了问题(我没有确定是类变量方面还是类定义正下方的位置导致的)


我仍然不明白为什么控制器代码是db迁移的一个因素。我很想知道这是否是我做错了什么,或者是rake、rails或其他方面的错误。只有在Heroku产品中发生了,rails 4.0.0

你能发布此迁移和包含“mytable”的迁移吗?添加了“mytable”“迁移,但我不知道在发生错误时如何确定正在运行的其他迁移。我实际上不确定…我会尝试这里列出的一些方法(包括完全删除你的应用)。你能用--trace运行迁移吗?这应该会提供失败时正在运行的迁移。现在有意义了,谢谢:)我刚刚遇到了与控制器代码有关的问题