RoR:表不存在错误

时间:2016-08-27 01:08:22

标签: mysql ruby-on-rails

我是新来的和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

这是db / migrate 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 =&gt;真正。但是现在当我执行rake 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)

1 个答案:

答案 0 :(得分:0)

我不确定为什么你有两个迁移文件,因为你应该只需要第一个。第一行包含以下行

create_table

create_table区块中,它不应该这样做。我认为将该行移动到该块下面的行(以便它位于change之外,但在add_index块内与其他 r1.SelAttributes.Size:=18; r1.SelText:=_n+'test1'; r1.SelAttributes.Size:=6; r1.SelText:=_n+#9; // <- tab here r1.SelAttributes.Size:=18; r1.SelText:=_n+'test2'; 行一起)应解决此问题。