简体   繁体   中英

rails db:migrate not working

I had a controller that I misnamed and ended up removing it from the command line via rails destroy controller . I ended up recreating everything but every time I have run rails db:migrate I get the following error.

SQLite3::SQLException: table "advertisements" already exists: CREATE TABLE "advertisements" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar, "copy" text, "price" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)

I'm at a loss in terms of what to do, I've been dealing with this one tiny mistake for the last two days and everytime I seem to get on the right track one, yep. If anyone can tell me why this error is happening and what I can do I would greatly appreciate it. Thanks in advance

Take a look at the db/migrate directory. Could it bee that you have two xxxxx_create_advertisements.rb files (where xxxxxx is a bunch of numbers, more precisely the timestamp for when this migration was created)?

If so, then remove one of them (if both have the same column definitions then it does not matter which one - unless you have foreign key dependencies).

And then try again.

您应该添加一个迁移来删除播发表。

The issue is because your 'destroy_controller' command somehow didn't delete the table 'advertisements'. There are 2 solutions :

  1. If you do not have any changes in the table then you don't need to recreate the table. Just comment out the migration in which you are creating table advertisements table.

  2. If you really want to re-create the table then add drop_table :advertisements in your migration before creating the table, this will delete your existing table and then continue with the migration.

You can find the migration in db/migrate/ folder with name like 'xxxxx_create_advertisements.rb'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM