0

Devise移行ファイルを使用しています。もともとはこんな感じでした:

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable

      # t.encryptable
      # t.confirmable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
      # t.token_authenticatable


      t.timestamps
    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
    # add_index :users, :authentication_token, :unique => true
  end

  def self.down
    drop_table :users
  end
end

このように3つの列を追加したかった:

t.first_name t.last_name t.organization_name

したがって、変更を加えると、移行ファイルは次のようになります。

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable
      t.first_name
      t.last_name
      t.organization_name
      # t.encryptable
      # t.confirmable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
      # t.token_authenticatable


      t.timestamps
    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
    # add_index :users, :authentication_token, :unique => true
  end

  def self.down
    drop_table :users
  end
end

次に、コマンドラインから次のコマンドを実行しました。

rake db:migrate

そして、結果のdbテーブルは、私が追加しようとした列を反映していませんでした。外観は次のとおりです。

describe users;
+------------------------+--------------+------+-----+---------+----------------+
| Field                  | Type         | Null | Key | Default | Extra          |
+------------------------+--------------+------+-----+---------+----------------+
| id                     | int(11)      | NO   | PRI | NULL    | auto_increment |
| email                  | varchar(255) | NO   | UNI |         |                |
| encrypted_password     | varchar(128) | NO   |     |         |                |
| reset_password_token   | varchar(255) | YES  | UNI | NULL    |                |
| reset_password_sent_at | datetime     | YES  |     | NULL    |                |
| remember_created_at    | datetime     | YES  |     | NULL    |                |
| sign_in_count          | int(11)      | YES  |     | 0       |                |
| current_sign_in_at     | datetime     | YES  |     | NULL    |                |
| last_sign_in_at        | datetime     | YES  |     | NULL    |                |
| current_sign_in_ip     | varchar(255) | YES  |     | NULL    |                |
| last_sign_in_ip        | varchar(255) | YES  |     | NULL    |                |
| created_at             | datetime     | YES  |     | NULL    |                |
| updated_at             | datetime     | YES  |     | NULL    |                |
+------------------------+--------------+------+-----+---------+----------------+

試みた変更が表示されない理由はありますか?変更を強制的に実行するにはどうすればよいですか?

ありがとう!!

4

2 に答える 2

3

移行ファイルを修正します。いくつかのエラーがあります。

...
t.first_name
t.last_name
t.organization_name
...

次のように変更します。

...
t.string :first_name
t.string :last_name
t.string :organization_name
...

詳細については、移行ガイドを確認してください。

この変更後、テーブルusersが存在しない場合は、次のことができますrake db:migrate。存在する場合は実行しますrake db:migrate:redo

ところで、テーブルの列の追加/削除/変更に別の移行を使用することをお勧めします。

于 2011-05-20T22:59:48.710 に答える
1

開発中であっても、既存の移行を変更しないことが最善ですが、許容される場合もあります。

この移行をすでに実行している場合はrake db:migrate、スキーマバージョンよりも新しい移行のみが実行されるため、を使用して再度実行されることはありません。

最後の移行を再度実行するには、rake db:migrate:redo

于 2011-05-20T22:30:54.637 に答える