简体   繁体   中英

Ruby On Rails Heroku db:migrate Aborted!

I've pushed my app to Heroku and now am trying to run '$ heroku rake db:migrate'. I get this error:

PGError: ERROR: relation "inventories" does not exist : SELECT "inventories".* FROM "inventories"

On my local machine everything works great. The local is using SQLite 3. Also, previous versions of the app worked just fine -- the previous versions did include the inventories model. Now, I've read ( almost ) every post on stackoverflow and on the web about this issue, but I still cannot find a way around it. Does anybody have any advice on getting this to work?

Ruby 1.9.2 ROR 3

UPDATE.. Here is the source to the migration that creates the inventories table:

class CreateInventories < ActiveRecord::Migration
  def self.up
    create_table :inventories do |t|
      t.decimal :initial_amount, :precision => 10, :scale => 2
      t.decimal :remaining_amount, :precision => 10, :scale => 2
      t.string :unit
      t.decimal :cost, :precision => 10, :scale => 2
      t.integer :type_id
      t.integer :brand_id
      t.integer :blend_id
      t.integer :user_id
      t.boolean :in

      t.timestamps
    end
  end

  def self.down
    drop_table :inventories
  end
end

The error indicates that the table does not exist remotely. See this: http://docs.heroku.com/database#common-issues-migrating-to-postgresql

I would expect a previous db:migrate to have created the table, unless database changes have occurred outside of the rake db task. I find that it's rather easy to get this out of sync. You can run specific migrations by specifying the migration method desired (up or down) and the timestamp of the migration to be executed. An example:

heroku rake db:migrate:up VERSION=20101108092153

This runs the migration generated on 11/8/2010 at 9:21:53 AM. You can browse your migrations in the db/ folder to find the migration that contains the missing table.

Have you used the Inventory model in your migration? Maybe you have an error in your migration, for example you edited the migration file after you migrated your local database?

In any case, running rake --trace db:migrate should show you the whole error message, together with stack trace - you will find the problematic line of code.

UPDATE:

In your stack trace (link is in the comment) there is one suspicious line:

...0-d4e1268c8981/mnt/config/environment.rb:5

What code is there?

SOLUTION: I finally figured out the issue. I had this method in my user model:

def self.search( id )
  @inventory = Inventory.where(:primary_user [id])
  @cups = Cup.where(:user_id [id])

  @inventory + @cups
end

For some reason, that caused errors. I updated it to use @user.inventories and @user.cups instead. Thanks everybody.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM