更新了 Rails3 迁移,但 rake:db migrate 后未显示结果

发布于 2024-11-08 14:46:14 字数 3406 浏览 0 评论 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.姓氏 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

生成的数据库表没有反映我尝试添加的列。它是这样的:

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    |                |
+------------------------+--------------+------+-----+---------+----------------+

知道为什么我尝试的更改没有显示出来吗?我如何强制改变发生?

谢谢!!

I am working with a Devise migration file. Originally it was like this:

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

I wanted to add 3 columns like this:

t.first_name
t.last_name
t.organization_name

So my migration file looked like this once I made the changes:

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

Then from command line I ran this command:

rake db:migrate

And the resulting db table did not reflect the columns I tried to add. Here is how it looks like:

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    |                |
+------------------------+--------------+------+-----+---------+----------------+

Any idea why my attempted changes are not showing up? How do I force the changes to happen?

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪若未夕 2024-11-15 14:46:14

修复您的迁移文件,它有一些错误:

...
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

顺便说一句,您最好使用另一个迁移来添加/删除/更改表上的列。

Fix your migration file, it has some errors:

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

Change it as follow:

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

You can check Migration guide for more information.

After this changes, if the table users doesn't exists, you can do rake db:migrate; if it exists do rake db:migrate:redo.

Btw is better to you use another migration for add/remove/change columns on your tables.

海夕 2024-11-15 14:46:14

最好不要更改现有的迁移,即使在开发中也是如此,但有时这是可以接受的。

如果您已经运行此迁移,则不会使用 rake db:migrate 再次运行它,因为只会运行比架构版本更新的迁移。

要再次运行上次迁移,您可以执行 rake db:migrate:redo

It is best not to change an existing migration, even in development, but sometime it is acceptable.

If you have already run this migration, it will not run again using rake db:migrate as only newer migrations than the schema version will be run.

To run the last migration again you can do rake db:migrate:redo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文