2
votes

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
2
Your error says that the movies table does not exist. Can you post your migration?John
You need to create a table called movies before running this migration.Luís Ramalho
do you have a migration before this one where you have something like create_table :movies? If you are missing the create_table, @luis-ramalho answer should work.John
Yes, I created the movies table in another migration. This one just added a new column, which seemed to be producing the Postgres errorSara
To make debugging easier I would suggest installing postgres on your dev machine and use it instead of sqlite. You might be able to replicate it locally if your db setup is closer to your production db.John

2 Answers

1
votes

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
0
votes

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.