【问题标题】:rails migration doesn't update databaserails迁移不更新数据库
【发布时间】:2014-03-12 21:04:04
【问题描述】:

我想在客户和项目之间建立关联,客户有_许多项目和项目属于_客户。但是迁移不会创建例如“client_id”。

这是我的模特:

class Client < ActiveRecord::Base
    has_many :projects, dependent: :destroy
end

class Project < ActiveRecord::Base
    belongs_to :client
end

这是我的迁移文件:

class CreateProjects < ActiveRecord::Migration
  def change
    create_table :projects do |t|
      t.string :name
      t.datetime :start_date
      t.datetime :end_date
      t.boolean :active

      t.timestamps
    end
  end
end

class CreateClients < ActiveRecord::Migration
  def change
    create_table :clients do |t|
      t.string :name

      t.timestamps
    end
  end
end

我应该手动操作吗?

【问题讨论】:

    标签: ruby-on-rails ruby database rails-activerecord rails-migrations


    【解决方案1】:

    您还需要在迁移中指定引用。引用被添加到具有外键的表中。

    class CreateProjects < ActiveRecord::Migration
      def change
        create_table :projects do |t|
          t.string :name
          t.datetime :start_date
          t.datetime :end_date
          t.boolean :active
    
          t.references :client # Add this line
    
          t.timestamps
        end
      end
    end
    

    【讨论】:

    • 我做到了,我还添加了 add_index :projects, :client_id 但它仍然不起作用......
    • 根据当前迁移版本,您可能需要回滚并重做此迁移。请记住,这样做会删除所有记录,因此如果您有任何数据,您可能需要考虑先备份表。
    【解决方案2】:

    如果你已经运行了迁移,你可以添加一个新的迁移

    rails generate migration AddClientToProjects client:references

    这将生成如下迁移:

    class AddClientToProjects < ActiveRecord::Migration
      def change
        add_reference :projects, :client, index: true
      end
    end
    

    然后做rake db:migrate


    如果您想在 CreateProjects 迁移本身中添加引用。

    然后执行以下操作:

    回滚迁移,如果它已经运行

    rake db:rollback VERSION=version_number

    在哪里,

    将 version_number 替换为迁移文件名中提到的版本号。

    例如:如果您的迁移文件名为 20140125190622_create_projects.rb 那么命令应该是

    rake db:rollback VERSION=20140125190622
    

    使用

    销毁当前迁移

    rails destroy migration CreateProjects

    并使用以下命令再次创建它:

    rails generate migration CreateProjects name start_date:datetime end_date:datetime active:boolean client:references

    这将创建如下迁移:

    class CreateProjects < ActiveRecord::Migration
      def change
        create_table :projects do |t|
          t.string :name
          t.datetime :start_date
          t.datetime :end_date
          t.boolean :active
    
          t.references :client, index:true # You could also add it manually to existing migration
    
          t.timestamps
        end
      end
    end
    

    在此之后运行rake db:migrate

    【讨论】:

    • 但是,如果CreateProjects 已经应用于生产数据库并且意图将关联添加为新功能,这将无济于事。
    • @muistooshort +1 我认为 OP 没有运行迁移。我会为此补充一点。感谢您指出这一点。
    【解决方案3】:

    您可以在此处找到当前问题的解决方案:http://guides.rubyonrails.org/association_basics.html

    您的项目迁移需要一行 t.integer :client_id

    or t.references :client from vee 也可以解决问题

    【讨论】:

      猜你喜欢
      • 2018-10-10
      • 1970-01-01
      • 2012-04-03
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-11
      相关资源
      最近更新 更多