【问题标题】:Updated a rails3 migration, but results dont show up after rake:db migrate更新了 rails3 迁移,但 rake:db 迁移后结果不显示
【发布时间】:2011-05-20 22:25:24
【问题描述】:

我正在处理一个设计迁移文件。原来是这样的:

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

知道为什么我尝试的更改没有出现吗?如何强制进行更改?

谢谢!!

【问题讨论】:

    标签: mysql ruby-on-rails ruby-on-rails-3 devise rails-migrations


    【解决方案1】:

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

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

    如下修改:

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

    您可以查看Migration guide了解更多信息。

    修改后,如果表users不存在,可以做rake db:migrate;如果存在,请执行rake db:migrate:redo

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 执行重做时出现此错误:rake aborted!发生错误,所有后续迁移都已取消:#<:connectionadapters::tabledefinition:0x925bc88> 的未定义方法“first_name”
      • 我还在 users.rb 文件中添加了这个 :first_name, :last_name, :organization_name,但似乎没有帮助。
      • 看起来您最好将所有更改存储到迁移中,手动向下迁移rake db:migrate:down VERSION=nnnnnnnnnnnn,重新应用更改并照常进行迁移。更改迁移通常不是一个好主意,最好只是在新的数据库中更改数据库。
      • 谢谢。 users.rb 文件呢?我应该改变它吗?我应该在向下迁移期间将其改回吗?
      • 保持原样应该没问题,看起来你没有在迁移中明确调用它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-13
      • 2018-08-01
      • 1970-01-01
      • 2017-07-24
      • 1970-01-01
      相关资源
      最近更新 更多