2

I have an existing rails project and I am using postgres database and after successful execution of bundle exec rake db:migrate command when I am going to execute the bundle exec rake db:seed command then it throws me the following error.

rake aborted! PG::DuplicateTable: ERROR: relation "schema_migrations" already exists

I have commented out the lines where the definition of the schema_migration table creation is stored such as in the development_structure.sql and test_structure.sql files. But it is still throwing me the error.

I don't know from where the schema_migration table is being created. There is no schema_migration.rb file in my project.

Pleas help me to fix this. Thanks in advance!

This is my error log.

  rake aborted!
  PG::DuplicateTable: ERROR:  relation "schema_migrations" already exists
 : CREATE TABLE "schema_migrations" ("version" character varying(255) NOT NULL) 
 /home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/connection_adapters/abstract_adapter.rb:227:in `rescue in log'
 /home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/connection_adapters/abstract_adapter.rb:204:in `log'
 /home/bit/Development/app2/application/lib/core_extensions/postgresql_adapter/reconnect.rb:26:in `execute'
 /home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/connection_adapters/abstract/schema_statements.rb:109:in `create_table'
 /home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/connection_adapters/abstract/schema_statements.rb:371:in `initialize_schema_migrations_table'
 /home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/migration.rb:441:in `initialize'
 /home/bit/.rvm/gems/ruby-2.1.5/gems/rails-2.3.18/lib/tasks/databases.rake:193:in `new'
 /home/bit/.rvm/gems/ruby-2.1.5/gems/rails-2.3.18/lib/tasks/databases.rake:193:in `block (2 levels) in <top (required)>'
 /home/bit/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `eval'
 /home/bit/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:seed => db:seed:common => db:seed:original =>    db:abort_if_pending_migrations
(See full trace by running task with --trace)
Abhradip
  • 393
  • 5
  • 27

1 Answers1

0

If it's OK to drop your data locally, try

rake db:drop db:create db:migrate

and then seed again. I've had similar issues in the past where I deleted a migration without properly rolling it back and this was the solution.

If you need to keep it, open the rails database (rails db from terminal) and try

drop table schema_migrations

(Run the migrations with db:migrate again after this)

Edit: This has not solved the issue yet, but it seems there may be other issues that need to be factored in. OP cannot launch rails console and receives

undefined local variable or method `vars' for #<Rails::Generator::Commands::Create:0x00000001ca2578>

as the error message each time - if anyone has ideas here, please add an answer!

kuwantum
  • 314
  • 2
  • 4
  • 14
  • Yes, It is still there. – Abhradip Jan 12 '18 at 07:32
  • You could also try `ActiveRecord::Migration.drop_table(:schema_migrations)` from the rails console to see if that works – kuwantum Jan 12 '18 at 07:33
  • after running that command ActiveRecord::Migration.drop_table(:schema_migrations) , what I need to do? Give the full sequence. – Abhradip Jan 12 '18 at 07:35
  • OK: Load rails console, run the above line, exit the rails console and run `rake db:migrate` again – kuwantum Jan 12 '18 at 07:36
  • rails console opened. I am using rails 2.1.5 , so I need to write script/console instead of writing rails c – Abhradip Jan 12 '18 at 09:10
  • Ahh, I didn't realise you were running a version of Rails that old! Did 'ActiveRecord::Migration.drop_table(:schema_migrations)' work? – kuwantum Jan 12 '18 at 11:51