0

我正在使用设计迁移文件。原来是这样的:

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 回答