Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relation already exists during rake migration

I have installed a blog engine to refinerycms which is working perfectly.

Now I have generated a migration with some table fields changes (of course not refinerycms or blog tables), but I'm getting an error:

== CreateBlogStructure: migrating ============================================
-- create_table("refinery_blog_posts", {:id=>true})
NOTICE: CREATE TABLE will create implicit sequence "refinery_blog_posts_id_seq1" for serial column "refinery_blog_posts.id"
rake aborted!
An error has occurred, this and all later migrations canceled:

PG::Error: ERROR: relation "refinery_blog_posts" already exists
: CREATE TABLE "refinery_blog_posts" ("id" serial primary key, "title" character varying(255), "body" text, "draft" boolean, "published_at" timestamp, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)

Tasks: TOP => db:migrate
(See full trace by running task with --trace)

like image 368
Vaibhav Rajput Avatar asked Sep 26 '12 12:09

Vaibhav Rajput


People also ask

How do I rollback a migration in Rails?

To check for status, run rails db:migrate:status . Then you'll have a good view of the migrations you want to remove. Then, run rails db:rollback to revert the changes one by one. After doing so, you can check the status again to be fully confident.

What does Rails DB Reset do?

rails db:reset:primary Drops and recreates the primary database from its schema for the current environment and loads the seeds. rails db:reset:secondary Drops and recreates the secondary database from its schema for the current environment and loads the seeds.


1 Answers

PG::Error: ERROR: relation “refinery_blog_posts” already exists

Pg is a Rails gem, the piece of code that allows communication between Rails and PostgreSQL. It relates your migrations to SQL tables, thus a relation error. So, what the error is saying is:

I'm trying to create table X, based on migration X, but table X already exists in the database.

Possible solutions:

  1. Create migrations that drop those, probably old, tables.
  2. Change the migration's name.
  3. Login to PostgreSQL and drop the table. Something like:

    $ psql -U username databasename
    

    then

    database_name=# drop table table-name;
    

    The exact commands might be a little different though.

like image 63
givanse Avatar answered Oct 24 '22 17:10

givanse