Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 2.3 App problems running database migration

Tags:

I have an old legacy app that I need to add some new tables to. I recently ran rake db:migrate to make the changes and go the following error:

PG::Error: ERROR: relation "schema_migrations" already exists : CREATE TABLE "schema_migrations" ("version" character varying(255) NOT NULL)

I looked in my schema.rb file and found for the "version" character and found it:

ActiveRecord::Schema.define(:version => 20170415055458) do .. end

but I don't understand why this is causing the error. I have not made any changes and can't figure this out. Any assistance appreciated.

I have tried dropping the database and rebuilding it but I get errors about relations not existing when they do and the schema.rb file is blank. It's like it does not know there are tables in the database.

like image 975
chell Avatar asked Apr 01 '19 00:04

chell


People also ask

How does Rails know which migration to run?

Rails uses this timestamp to determine which migration should be run and in what order, so if you're copying a migration from another application or generate a file yourself, be aware of its position in the order. This generator can do much more than append a timestamp to the file name.

How do you run down migration in Rails?

To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users. rb , you would use 20160515085959 as the version number.

How does Rails migration work internally?

A Rails migration is a tool for changing an application's database schema. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). The code is database-independent, so you can easily move your app to a new platform.

How do I migrate a database in Ruby on Rails?

Go to db/migrate subdirectory of your application and edit each file one by one using any simple text editor. The ID column will be created automatically, so don't do it here as well. The method self. up is used when migrating to a new version, self.


1 Answers

According to https://gist.github.com/TylerRick/9811465 and https://www.redmine.org/boards/2/topics/6051, it could be that the schema_migrations table is part of the public postgresql schema, while the database.yml doesn't specify it. See if you already have a value for schema_search_path and if adding public to it helps.

development:
  adapter: postgresql
  database: project_development
  schema_search_path: public

or

  schema_search_path: "existing,public"

More about schema_search_path: https://til.hashrocket.com/posts/5aa2892b43-set-schema-search-path

like image 59
Stéphane Bruckert Avatar answered Sep 28 '22 06:09

Stéphane Bruckert