PG::DuplicateTable: ERROR: relation “posts” already exists

后端 未结 6 934
温柔的废话
温柔的废话 2020-12-13 19:58

When I run rake db:migrate I get following output:

== 20141219011612 CreatePost: migrating ======================================= -- create_table("

相关标签:
6条回答
  • 2020-12-13 20:32

    In case this helps anyone else, I realized that I had been using a schema.rb file that was generated while using MySQL. After transitioning to Postgres, I simply forgot I would need to run rake db:migrate before I could use rake db:schema:load

    0 讨论(0)
  • 2020-12-13 20:47

    Somehow, you ended up with a table named 'posts' in your database. Perhaps from a prior migration that you deleted without rolling back? If you don't care about any of your data in the database, you can run

    rake db:drop db:create db:migrate
    

    to bring your development database inline with your current migrations.

    If you have data in other tables you don't want to lose, open the database console and drop the posts table manually:

    $ rails db
    
    # drop table posts;
    

    Then run db:migrate again.

    0 讨论(0)
  • 2020-12-13 20:49

    Check your db/schema.rb

    You most likely have the same table being created there in addition to a migration in db/migrate/[timestamp]your_migration

    You can delete the db/migrate/[timestamp]your_migration if it is a duplicate of the one found in the schema and it should work.

    0 讨论(0)
  • 2020-12-13 20:49

    kill the current postgres process:

    sudo kill -9 `ps -u postgres -o pid` 
    

    start postgres again:

    brew services start postgres
    

    drop, create, and migrate table:

    rails db:drop db:create db:migrate
    
    0 讨论(0)
  • 2020-12-13 20:51

    For those who didn't get your answer above


    In my case, I had been working on a feature a month ago the field happens to be created at that time. Now when I try to run migration rake db: migrate I see this error. I know and am sure that this is not here due to any mistake.

    I also tried to rollback that particular migration

    rake db:migrate:down VERSION=20200526083835
    

    but due to some reason, it did nothing, and to move further I had to comment out the up method in that migration file.

    # frozen_string_literal: true
    
    class AddColToAccounts < ActiveRecord::Migration
      def up
        # execute 'commit;'
        #
        # add_column :accounts, :col, :boolean,
        #            null: false,
        #            default: false
        
    
      end
    
      def down
        execute 'commit;'
    
        remove_column :accounts, :col
        
      end
    end
    
    
    

    And, now I am able to run the migrations.

    At last, I undo the commenting thing and I am done.

    Thanks

    0 讨论(0)
  • 2020-12-13 20:53

    One of the hack I found was to put pry before you are creating the table on the migration file.

    require 'pry'
    binding.pry
    
    create_table :your_table_name
    

    and drop that table:

    drop_table :your_table_name
    

    After that you can remove the drop_table line and it will work fine!

    0 讨论(0)
提交回复
热议问题