我是新来的,在RoR。我坐了5个小时,但程序却出了问题。我严格遵循了一个教程,这发生了:

Ruby版本:2.2.4
Rails版本:5.0.0.1
设计版本:4.2.0

C:\RailsInstaller\Ruby2.2.0\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/bin/rails db:migrate
rails aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Table 'rubyandroid_development.users' doesn't exist: CREATE UNIQUE INDEX `index_users_on_authentication_token`  ON `users` (`authentication_token`)
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/db/migrate/20160826225028_devise_create_users.rb:12:in `block in change'
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/db/migrate/20160826225028_devise_create_users.rb:3:in `change'
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/bin/rails:4:in `require'
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/bin/rails:4:in `<top (required)>'
-e:1:in `load'
-e:1:in `<main>'
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'rubyandroid_development.users' doesn't exist: CREATE UNIQUE INDEX `index_users_on_authentication_token`  ON `users` (`authentication_token`)
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/db/migrate/20160826225028_devise_create_users.rb:12:in `block in change'
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/db/migrate/20160826225028_devise_create_users.rb:3:in `change'
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/bin/rails:4:in `require'
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/bin/rails:4:in `<top (required)>'
-e:1:in `load'
-e:1:in `<main>'
Mysql2::Error: Table 'rubyandroid_development.users' doesn't exist
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/db/migrate/20160826225028_devise_create_users.rb:12:in `block in change'
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/db/migrate/20160826225028_devise_create_users.rb:3:in `change'
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/bin/rails:4:in `require'
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/bin/rails:4:in `<top (required)>'
-e:1:in `load'
-e:1:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
== 20160826225028 DeviseCreateUsers: migrating ================================
-- create_table(:users)
-- add_index(:users, :authentication_token, {:unique=>true})

Process finished with exit code 1


这是db / migrate / devise_create_users代码:

class DeviseCreateUsers < ActiveRecord::Migration[5.0]
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      t.string :name, :null => false, :default => ""

      #token hinzugefügt
      t.string :authentication_token
      add_index :users, :authentication_token, :unique => true


      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end


这是数据库/迁移add_devise_to_users

class AddDeviseToUsers < ActiveRecord::Migration[5.0]
  def self.up
    change_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      # Uncomment below if timestamps were not included in your original model.
      # t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end

  def self.down
    # By default, we don't want to make any assumption about how to roll back a migration when your
    # model already existed. Please edit below which fields you would like to remove in this migration.
    raise ActiveRecord::IrreversibleMigration
  end
end


更新:我忘了不推荐该行:
      add_index:users,:authentication_token,:unique => true。但是现在当我倾斜db:migrate时,会发生以下错误:

Mysql2::Error: Duplicate column name 'email': ALTER TABLE `users` ADD `email` varchar(255) DEFAULT '' NOT NULL
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/db/migrate/20160827004755_add_devise_to_users.rb:5:in `block in up'
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/db/migrate/20160827004755_add_devise_to_users.rb:3:in `up'
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/bin/rails:4:in `require'
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/bin/rails:4:in `<top (required)>'
-e:1:in `load'
-e:1:in `<main>'
ActiveRecord::StatementInvalid: Mysql2::Error: Duplicate column name 'email': ALTER TABLE `users` ADD `email` varchar(255) DEFAULT '' NOT NULL
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/db/migrate/20160827004755_add_devise_to_users.rb:5:in `block in up'
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/db/migrate/20160827004755_add_devise_to_users.rb:3:in `up'
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/bin/rails:4:in `require'
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/bin/rails:4:in `<top (required)>'
-e:1:in `load'
-e:1:in `<main>'
Mysql2::Error: Duplicate column name 'email'
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/db/migrate/20160827004755_add_devise_to_users.rb:5:in `block in up'
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/db/migrate/20160827004755_add_devise_to_users.rb:3:in `up'
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/bin/rails:4:in `require'
C:/Benutzer/Clemens/RubymineProjects/RubyAndroid/bin/rails:4:in `<top (required)>'
-e:1:in `load'
-e:1:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
== 20160827004755 AddDeviseToUsers: migrating =================================
-- change_table(:users)

最佳答案

我不确定为什么要为此使用两个迁移文件,因为您只需要第一个迁移文件。第一个包含以下行

add_index :users, :authentication_token, :unique => true


create_table块中不应该这样做。我认为将那条线移动到该块下方的行(以便它位于create_table之外,但与其他change行一起位于add_index块内)应该可以解决此问题。

关于mysql - RoR:表格不存在错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39176545/

10-11 03:16