Skip to content
  • Yves Senn's avatar
    0230ad68
    Add a `foreign_key` option to `references` and `add_reference`. [Sean Griffin & Yves Senn] · 0230ad68
    Yves Senn authored
    Closes #18664.
    
    This is a backport of:
    
    * 174f5ed5 add test to ensure `remove_reference` with index and fk is invertable. [Yves Senn]
    * a893718c fix `remove_reference` with `foreign_key: true` on MySQL. #18664. [Yves Senn]
    * e550bbf8 Changelog for 99a6f9e6 [Sean Griffin]
    * 84927431 Don't wrap `create_table` in a transaction for tests which run on MySQL [Sean Griffin]
    * b7d7e0b1 Add test missed by a03ea684 [Sean Griffin]
    * a03ea684 Use the new `foreign_key` option on `references` in generators [Sean Griffin]
    * 82afeaf2 Add `foreign_key` as an option to `references` for `change_table` [Sean Griffin]
    * 99a6f9e6 Add a `foreign_key` option to `references` while creating the table [Sean Griffin]
    
    =====================================================
    
    Rather than having to do:
    
        create_table :posts do |t|
          t.references :user
        end
    
        add_foreign_key :posts, :users
    
    You can instead do:
    
        create_table :posts do |t|
          t.references :user, foreign_key: true
        end
    
    Similar to the `index` option, you can also pass a hash. This will be
    passed as the options to `add_foreign_key`. e.g.:
    
        create_table :posts do |t|
          t.references :user, foreign_key: { primary_key: :other_id }
        end
    
    is equivalent to
    
        create_table :posts do |t|
          t.references :user
        end
    
        add_foreign_key :posts, :users, primary_key: :other_id
    0230ad68
    Add a `foreign_key` option to `references` and `add_reference`. [Sean Griffin & Yves Senn]
    Yves Senn authored
    Closes #18664.
    
    This is a backport of:
    
    * 174f5ed5 add test to ensure `remove_reference` with index and fk is invertable. [Yves Senn]
    * a893718c fix `remove_reference` with `foreign_key: true` on MySQL. #18664. [Yves Senn]
    * e550bbf8 Changelog for 99a6f9e6 [Sean Griffin]
    * 84927431 Don't wrap `create_table` in a transaction for tests which run on MySQL [Sean Griffin]
    * b7d7e0b1 Add test missed by a03ea684 [Sean Griffin]
    * a03ea684 Use the new `foreign_key` option on `references` in generators [Sean Griffin]
    * 82afeaf2 Add `foreign_key` as an option to `references` for `change_table` [Sean Griffin]
    * 99a6f9e6 Add a `foreign_key` option to `references` while creating the table [Sean Griffin]
    
    =====================================================
    
    Rather than having to do:
    
        create_table :posts do |t|
          t.references :user
        end
    
        add_foreign_key :posts, :users
    
    You can instead do:
    
        create_table :posts do |t|
          t.references :user, foreign_key: true
        end
    
    Similar to the `index` option, you can also pass a hash. This will be
    passed as the options to `add_foreign_key`. e.g.:
    
        create_table :posts do |t|
          t.references :user, foreign_key: { primary_key: :other_id }
        end
    
    is equivalent to
    
        create_table :posts do |t|
          t.references :user
        end
    
        add_foreign_key :posts, :users, primary_key: :other_id
Loading