Cannot run rake db:migrate, relation does not exis

2020-03-26 05:03发布

问题:

I am trying to get a working app to work on linux 10.04 running on vagrant

I installed all relevant gems, installed postgresql 9.1.9 and when I am trying to run rake db:migrate I am getting

rake aborted!
PG::Error: ERROR:  relation "messages" does not exist
LINE 5:              WHERE a.attrelid = '"messages"'::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 = '"messages"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:1161:in `exec'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:1161:in `exec_no_cache'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:660:in `block in exec_query'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/rack-mini-profiler-0.1.26/Ruby/lib/patches/sql_patches.rb:246:in `log_with_miniprofiler'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/newrelic_rpm-3.6.0.83/lib/new_relic/agent/instrumentation/active_record.rb:36:in `block in log_with_newrelic_instrumentation'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/newrelic_rpm-3.6.0.83/lib/new_relic/agent/method_tracer.rb:235:in `trace_execution_scoped'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/newrelic_rpm-3.6.0.83/lib/new_relic/agent/instrumentation/active_record.rb:33:in `log_with_newrelic_instrumentation'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:659:in `exec_query'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:1286:in `column_definitions'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/connection_adapters/postgresql_adapter.rb:855:in `columns'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/connection_adapters/schema_cache.rb:12:in `block in initialize'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/model_schema.rb:228:in `yield'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/model_schema.rb:228:in `default'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/model_schema.rb:228:in `columns'

/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/model_schema.rb:237:in `columns_hash'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/inheritance.rb:19:in `descends_from_active_record?'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/inheritance.rb:25:in `finder_needs_type_condition?'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/base.rb:455:in `relation'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/scoping/named.rb:37:in `scoped'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activerecord-3.2.13/lib/active_record/querying.rb:9:in `where'
/home/vagrant/myapp/app/models/message.rb:29:in `<class:Message>'
/home/vagrant/myapp/app/models/message.rb:25:in `<top (required)>'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:469:in `load'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:469:in `block in load_file'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:639:in `new_constants_in'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:468:in `load_file'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:353:in `require_or_load'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:502:in `load_missing_constant'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:192:in `block in const_missing'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:190:in `each'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:190:in `const_missing'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:230:in `block in constantize'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `each'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `constantize'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/acts-as-messageable-0.4.8/lib/acts-as-messageable/model.rb:34:in `acts_as_messageable'
/home/vagrant/myapp/app/models/user.rb:123:in `<class:User>'
/home/vagrant/myapp/app/models/user.rb:45:in `<top (required)>'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:469:in `load'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:469:in `block in load_file'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:639:in `new_constants_in'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:468:in `load_file'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:353:in `require_or_load'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:502:in `load_missing_constant'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:192:in `block in const_missing'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:190:in `each'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:190:in `const_missing'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:230:in `block in constantize'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `each'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `constantize'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:554:in `get'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:588:in `constantize'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/lib/devise.rb:272:in `get'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/lib/devise/mapping.rb:77:in `to'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/lib/devise/mapping.rb:72:in `modules'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/lib/devise/mapping.rb:89:in `routes'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/lib/devise/mapping.rb:156:in `default_used_route'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/lib/devise/mapping.rb:66:in `initialize'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/lib/devise.rb:306:in `new'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/lib/devise.rb:306:in `add_mapping'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/lib/devise/rails/routes.rb:208:in `block in devise_for'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/lib/devise/rails/routes.rb:207:in `each'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/devise-2.2.4/lib/devise/rails/routes.rb:207:in `devise_for'
/home/vagrant/myapp/config/routes.rb:27:in `block in <top (required)>'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:289:in `instance_exec'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:289:in `eval_block'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:267:in `draw'
/home/vagrant/myapp/config/routes.rb:1:in `<top (required)>'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `block in load'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in `load_dependency'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/application/routes_reloader.rb:40:in `each'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/application/routes_reloader.rb:40:in `load_paths'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/application/routes_reloader.rb:16:in `reload!'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/application/routes_reloader.rb:26:in `block in updater'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/file_update_checker.rb:78:in `call'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/file_update_checker.rb:78:in `execute'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/application/routes_reloader.rb:27:in `updater'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/application/finisher.rb:66:in `block in <module:Finisher>'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `instance_exec'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `run'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/initializable.rb:55:in `block in run_initializers'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `each'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `run_initializers'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/application.rb:136:in `initialize!'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/railtie/configurable.rb:30:in `method_missing'
/home/vagrant/myapp/config/environment.rb:5:in `<top (required)>'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `require'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `block in require'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in `load_dependency'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:251:in `require'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/application.rb:103:in `require_environment!'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.13/lib/rails/application.rb:297:in `block (2 levels) in initialize_tasks'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/task.rb:246:in `call'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/task.rb:241:in `each'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/task.rb:241:in `execute'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/task.rb:184:in `block in invoke_with_call_chain'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/task.rb:205:in `block in invoke_prerequisites'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/task.rb:203:in `each'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/task.rb:203:in `invoke_prerequisites'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/task.rb:183:in `block in invoke_with_call_chain'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/task.rb:170:in `invoke'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/application.rb:143:in `invoke_task'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/application.rb:101:in `each'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/application.rb:101:in `block in top_level'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/application.rb:110:in `run_with_threads'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/application.rb:95:in `top_level'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/application.rb:73:in `block in run'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429@global/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/bin/ruby_noexec_wrapper:14:in `eval'
/home/vagrant/.rvm/gems/ruby-1.9.3-p429/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:migrate => environment

running rake db:create:all tells me all tables have already been created

I don't know if it matters or not, but I am using devise 2.2.4 and latest acts_as_messageable.

note that this app does work on a windows development machine and production in heroku.. I just want to make it run on vagrant as well..

my database.yml is

common: &common

      adapter: postgresql
      enconding: utf8
      host: localhost
      username: postgres
      password: 1234
      pool: 5
      timeout: 5000
    development:
      <<: *common
      database: myapp_dev
      port: 5432
    test:
      <<: *common
      database: myapp_test

    production:
      <<: *common
      database: myapp_prod

I suspect it has to do with some postgres configuration, but I am not sure.. Couldn't find any solution to this yet

When checking psql I get this

postgres=# \l
                                   List of databases
    Name     |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-------------+----------+----------+-------------+-------------+-----------------------
 postgres    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
             |          |          |             |             | postgres=CTc/postgres
 template1   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
             |          |          |             |             | postgres=CTc/postgres
 myapp_dev  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 myapp_prod | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 myapp_test | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(6 rows)

postgres=# \d myapp_dev
Did not find any relation named "myapp_dev".

so something is wrong here I think.. is there a solution?

Note - this happens with rake db tasks such as schema:load, setup..

In case this is needed - here are the relevant lines from the error relating to acts_as_messageable, including the migration file

user: line 120-123

acts_as_messageable :table_name => "messages", 
                    :required   => :body,               
                    :class_name => "Message",     
                    :dependent  => :destroy        

Message class (line 25-29)

class Message < ActsAsMessageable::Message

attr_accessible :sender_open, :reciever_open
scope :unread_by, lambda{ |user| where("reciever_open = false AND received_messageable_id = ?", user.id)}
scope :unread, where('reciever_open = false')

the migration file - 20121127082447_create_messages_table.rb (its not first or last, somewhere in the middle)

class CreateMessagesTable < ActiveRecord::Migration
  def self.up
    create_table :messages do |t|
      t.string :topic
      t.text :body
      t.references :received_messageable, :polymorphic => true
      t.references :sent_messageable, :polymorphic => true
      t.boolean :opened, :default => false
      t.boolean :recipient_delete, :default => false
      t.boolean :sender_delete, :default => false
      t.timestamps

      # ancestry
      t.string :ancestry
    end

    add_index :messages, [:sent_messageable_id, :received_messageable_id], :name => "acts_as_messageable_ids"
    add_index :messages, :ancestry
  end

  def self.down
    drop_table :messages
  end
end

回答1:

The Routes file in Rails uses device_for, which loads the User model, which in turn has acts_as_messagable and loads the Message class. Line #29 in Message class says:

scope :unread, where('reciever_open = false')

The where method is triggering a column lookup, which fails because the migrations haven't run yet! Try one of these two:

Change this to:

scope :unread, where(:reciever_open => false)

Or if that also triggers a column lookup, then say:

scope :unread, lambda { where('reciever_open = false') }



回答2:

Just trying to check on the obvious here, but have you tried running rake db:drop? Whenever I make changes to my migrations, I need to drop all of my DB tables before I try to create and migrate them.

So the full operation would be rake db:drop db:create db:migrate

Oh, and you can also try to specify the environment (I sometimes need to do that on test environments), e.g. rake RAILS_ENV=test db:drop db:create db:migrate