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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With