简体   繁体   中英

Mysql2::Error: Table “Admin_Users” already exists

--------------------------EDIT #1--------------------------

I found a way around my problem:

The Problem:

I typo'd "admin_user", when it should have been "admin_users" in my migration, and ran rake db:migrate. Giving an ERROR before completing the migration.

A Solution:

rake db:drop #drops the database
rake db:create #re-creates the database

A Question:

How would I have fixed migrating a typo without dropping my whole database and re-creating it? Obviously I don't want to be deleting data with a real project.

EDIT #2 Answer to my question^:

You can go into your code and comment the code that has been already migrated, which will finish the rest of your migration when you run:

rake db:migrate

END OF EDIT #2

--------------------------END OF EDIT #1--------------------------

I'm coming across a noobie MySQL problem using Ruby On Rails 4.

class AlterUsers < ActiveRecord::Migration
def up
    rename_table("users", "admin_users")
    add_column("admin_users", "username", :string, :limit => 25, :after => "email")
    change_column("admin_users", "email", :string, :limit => 100)
    rename_column("admin_users", "password", "hashed_password")
    puts "*** Adding an index is next ***"
    add_index("admin_users", "username")
end

def down
    remove_index("admin_users", "username")
    rename_column("admin_users", "hashed_password", "password")
    change_column("admin_users", "email", :string, :default => "", :null => false)
    remove_column("admin_users", "username")
    rename_table("admin_users", "users")
end
end

Then I ran:

rake db:migrate

Which then migrated "Create Users", but gave me an ERROR when hitting the Alter Users Migration:

*** Adding an index is next ***
-- add_index("admin_user", "username")
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: Table 'simple_cms_development.admin_user' doesn't exist: CREATE  INDEX `index_admin_user_on_username`  ON `admin_user` (`username`)

I then tried:

rake db:migrate:status

Which resulted in:

up     20150911203937  Do nothing yet
up     20150911204213  Create users
down    20150911212536  Alter users

So I figured if I just reverted back to all DOWN status by using:

rake db:migrate VERSION=0

But then gave me another Error:

Mysql2::Error: Unknown table 'users': DROP TABLE `users`
/db/migrate/20150911204213_create_users.rb:16:in `down'

Obviously this is just a practice Database and RoR Project in general, but how can I fix this Error and in the BEST possible way so I won't delete anything important (in future SQL projects).

I've read these threads before posting here:

http://stackoverflow.com/questions/24988889/mysql2error-table-conversations-already-exists
http://stackoverflow.com/questions/27876009/ruby-on-rails-mysql2error-table-pages-already-exists-create-table-pages

Thank you for any answers/advice!

您的问题没有道理,您的错误(以及明显的db:migrate输出)显示了一个问题,因为没有"admin_user" (单数)表,但是您的迁移却显示了add_index("admin_users"... (复数)。最重要的是,问题的错误从未在您的文章中提及。绝对看起来您一团糟,我的建议是删除数据库并从头开始。

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