【问题标题】:Rails Migration Not Matching ModelRails 迁移不匹配模型
【发布时间】:2013-03-20 04:09:42
【问题描述】:

我无法理解迁移如何根据模型自行更新。

例如,我已经设计了设置,我想删除电子邮件字段并且只有一个用户名和密码字段。

所以,我做到了

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :username, :password
  # attr_accessible :title, :body
end

但是,即使在

之后,迁移文件也不会反映这些更改
rake db:migrate

 rake db:reset

迁移文件仍有电子邮件字段

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_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
      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 # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at

      ## Token authenticatable
      # t.string :authentication_token


      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
end

我还尝试删除迁移文件,以便 Rails 知道创建一个新文件。但是,这不起作用,并且迁移仍然是从某个地方运行的?这些迁移是从哪里运行的?请注意,我的 db/migrate 文件当时是空的。

-- create_table("users", {:force=>true})
   -> 0.2476s
-- add_index("users", ["email"], {:unique=>true, :name=>"index_users_on_email"})
   -> 0.1832s
-- add_index("users", ["reset_password_token"], {:unique=>true, :name=>"index_users_on_reset_password_token"})
   -> 0.1832s
-- initialize_schema_migrations_table()
   -> 0.3776s
-- assume_migrated_upto_version(20130320033132, ["/home/Portfolio Rails/portfolio/db/migrate"])
   -> 0.1532s

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3


    【解决方案1】:

    你把这一切都搞反了。迁移是磁盘上的文件。它们从不“更新自己”,尽管您可以要求 Rails 为您生成它们,但它会根据您的输入而不是您的数据库或模型的状态来生成它们。

    更改不会从模型流向迁移,而是从迁移通过数据库流向模型。 (开发人员)负责通过引入 迁移来对数据库进行更改。如果你想删除一个字段,你需要创建一个新的迁移来删除该列。

    运行rake db:migrate 与您认为的相反。它复制状态你的迁移,而不是他们。它将查看db/migrate 并查看是否有任何新文件包含有关如何修改数据库状态的说明。

    【讨论】:

      【解决方案2】:

      迁移文件只是您对架构所做修改的书面记录。修改它们对数据库本身没有任何作用。

      您需要创建一个移除电子邮件字段的迁移:

      rails generate migration remove_email_from_users email:string
      rake db:migrate
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-24
        • 2013-03-23
        • 2017-05-19
        相关资源
        最近更新 更多