Reputation: 31
I got some problems about my database.
I've generated a model Comment once, but this Comment migration not what I wanted, so I drop it, and succeeded to drop. Now I try to re-generate it, and keep running rails db:migrate
, but something went wrong.
It shows :
"PG::DuplicateTable: ERROR: relation "comments" already exists",
I've checked my schema.rb
, did not have this comment table.
My database using "psql", and Rails version is "Rails 6.0.2.2". I've been searched related problem, but seems like not what I faced.
Upvotes: 2
Views: 2386
Reputation: 31
according your description, I have also encountered this, this is what I tried and solved:
psql
rails db
\d:
drop table tablename;
hope to solve your problem
Upvotes: 3
Reputation: 1183
If the data is not important you could just start fresh by dropping the current database and create a new one
rake db:drop
rake db:create
rake db:migrate
If you are sure that those migrations already have been run before, then you can go and modify the schema migrations table directly where rails stores all migrations that have been run already.
First log into your postgres and choose the correct database
sudo -u postgres psql
\c db_name
then to view the current migrations in the schema table
select * from schema_migrations;
that will show you the current migrations that rails considers as done.
Then check you migrations folder, get the version of all the migration files you want to skip running, the file name starts with the version
for example
20200401212538_add_country_name.rb
the first numeric 20200401212538
part is the version
then insert them into the migrations table
insert into schema_migrations VALUES ('version_of_migration');
Or you could also delete migrations so you could re-run them if you want to.
After that running rake db:migrate
should work just fine without trying to recreate the tables.
Another solutions you might consider is that in your migrations you check first if table exists or not before trying to create it.
Upvotes: 0