简体   繁体   中英

Postgres error when I try to run heroku run rake db:migrate

My sqlite3 database works fine in development but when I try to migrate it to production I get the following error:

PG::Error: ERROR: relation "movies" does not exist : ALTER TABLE "movies" ADD COLUMN "production_company" character varying(255)/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:652:in `async_exec'

I know a few people have posted about this but nothing I've tried seems to work. Anyone know how I might fix this?

Here's the migration:

class AddProductionCompanyToMovies < ActiveRecord::Migration
  def change
    add_column :movies, :production_company, :string, :limit => nil
  end
end

Here's my schema.rb file if this helps:

ActiveRecord::Schema.define(:version => 20130331014529) do

create_table "movies", :force => true do |t|
t.string   "title"
t.string   "actor_1"
t.string   "locations"
t.string   "release_year"
t.string   "string"
t.string   "actor_2"
t.string   "actor_3"
t.string   "writer"
t.string   "director"
t.datetime "created_at",         :null => false
t.datetime "updated_at",         :null => false
t.string   "production_company"
t.string   "distributor"
t.string   "fun_facts"
end

end

Here's the migration where I create the movies table:

class Movies < ActiveRecord::Migration
  def up
  end

  def down
  end
end

It's not the best approach but a quick fix would be to replace that migration with this:

class AddProductionCompanyToMovies < ActiveRecord::Migration
  def change
    create_table :movies do |t|
      t.string :production_company

      t.timestamps
    end
  end
end

Your migration where you create the movie table is incorrect. The up and down methods you have don't do anything. Because of this, there is no movie table to add the production_company column to.

You need something like this;

class Movies < ActiveRecord::Migration
  def change
    create_table :movies do |t|
      t.string  :title
      t.string  :actor
      .
      .    #add your columns you want in your initial migration here
      .
  end
end

I can't say why things worked in development in SQLite but at some point you successfully created the movies table and then maybe you altered the migration after that. This is easy to do (I've done it!).

A lot of people recommend that when you set up production you don't run your migrations to set up the database, but instead use rake db:schema:load (in fact, if you read the comments at the top of your db/schema.rb file it specifically describes this).

Another point is that a lot of people recommend having the same database in development as in production as there are subtle differences that can lead to unexpected problems in production (this is not what has caused your issue). If you're only just starting out then don't worry about it for now; it's just one more headache to set up PostgreSQL on your own machine; but it's something to keep in mind as you progress.

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