Rails 3 migrations: Adding reference column?

后端 未结 10 2061
余生分开走
余生分开走 2020-11-30 17:15

If I create a new rails 3 migration with (for example)

rails g migration tester title:tester user:references

, everything works fine...howe

相关标签:
10条回答
  • 2020-11-30 17:39

    For Rails 4

    The generator accepts column type as references (also available as belongs_to).

    This migration will create a user_id column and appropriate index:

    $ rails g migration AddUserRefToProducts user:references 
    

    generates:

    class AddUserRefToProducts < ActiveRecord::Migration
      def change
        add_reference :products, :user, index: true
      end
    end
    

    http://guides.rubyonrails.org/active_record_migrations.html#creating-a-standalone-migration

    For Rails 3

    Helper is called references (also available as belongs_to).

    This migration will create a category_id column of the appropriate type. Note that you pass the model name, not the column name. Active Record adds the _id for you.

    change_table :products do |t|
      t.references :category
    end
    

    If you have polymorphic belongs_to associations then references will add both of the columns required:

    change_table :products do |t|
      t.references :attachment, :polymorphic => {:default => 'Photo'}
    end
    

    Will add an attachment_id column and a string attachment_type column with a default value of Photo.

    http://guides.rubyonrails.org/v3.2.21/migrations.html#creating-a-standalone-migration

    0 讨论(0)
  • 2020-11-30 17:40

    When adding a column you need to make that column an integer and if possible stick with rails conventions. So for your case I am assuming you already have a Tester and User models, and testers and users tables.

    To add the foreign key you need to create an integer column with the name user_id (convention):

    add_column :tester, :user_id, :integer
    

    Then add a belongs_to to the tester model:

    class Tester < ActiveRecord::Base
      belongs_to :user
    end
    

    And you might also want to add an index for the foreign key (this is something the references already does for you):

    add_index :tester, :user_id
    
    0 讨论(0)
  • 2020-11-30 17:42

    With the two previous steps stated above, you're still missing the foreign key constraint. This should work:

      class AddUserReferenceToTester < ActiveRecord::Migration
          def change
              add_column :testers, :user_id, :integer, references: :users
          end
      end
    
    0 讨论(0)
  • 2020-11-30 17:43

    Please note that you will most likely need an index on that column too.

    class AddUserReferenceToTester < ActiveRecord::Migration
      def change
        add_column :testers, :user_id, :integer
        add_index  :testers, :user_id
      end
    end
    
    0 讨论(0)
  • 2020-11-30 17:45

    That will do the trick:

    rails g migration add_user_to_tester user_id:integer:index
    
    0 讨论(0)
  • 2020-11-30 17:52

    Running rails g migration AddUserRefToSponsors user:references will generate the following migration:

    def change
      add_reference :sponsors, :user, index: true
    end
    
    0 讨论(0)
提交回复
热议问题