shou2017.com
JP

PG::DuplicateTable: ERROR

Wed Jul 26, 2017
Sat Aug 10, 2024

When I tried to migrate, I got the error message below.

Mac:$ rake db:migrate
== 20170726082613 CreateGamble: migrating =====================================
-- create_table(:gambles)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::DuplicateTable: ERROR:  relation "gambles" already exists
: CREATE TABLE "gambles" ("id" serial primary key, "a_team" character varying, "b_team" character varying, "game" timestamp, "event" character varying, "place" character varying, "description" character varying)
〜〜
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

The meaning of this error is like saying that the same TABLE is already there.

If you are in this situation, simply delete the TABLE and you are good to go.

Type the following in the terminal.

$ rails db

Then # is displayed, where you type \d.

petipeti_development=# \d
               List of relations
 Schema |       Name        |   Type   | Owner
--------+-------------------+----------+-------
 public | gambles           | table    | boku
 public | gambles_id_seq    | sequence | boku
 public | schema_migrations | table    | boku
(3 rows)

In this case, I want to delete the one whose Name is “gambles” and whose Type is “table”, so I enter the following.

petipeti_development=# drop table gambles;

Check if you have done it correctly.

petipeti_development=# \d
             List of relations
 Schema |       Name        | Type  | Owner
--------+-------------------+-------+-------
 public | schema_migrations | table | boku
(1 row)

It works, so all that remains is,

$ rake db:migrate

and you’re good to go.

See Also