Skip to content
  • Adrianna Chang's avatar
    2fdd1ad6
    Infer primary_key: :id on associations with composite primary key models · 2fdd1ad6
    Adrianna Chang authored
    Previously, a composite primary key model would need to specify
    either a `primary_key` or `query_constraints` option on its associations.
    Without it, a `CompositePrimaryKeyMismatchError` would be raised.
    
    Most of the time, the composite primary key includes the `:id` column,
    and associations already expect that `:id` is being used as the
    primary key for the association (and have a corresponding foreign_key
    column set on the associated model).
    
    Rather than requiring users to define `primary_key: :id` throughout
    an application with composite primary key associations, we can infer
    that the `:id` column should be used. Users can still override this
    behaviour by specifying a `primary_key:` or `query_constraints:` on
    the association.
    
    Note that, if the composite primary key for a model does _not_ include
    `:id`, Rails won't infer the primary key for any related associations,
    and users must still specify `query_constraints` or `primary_key`.
    
    Prior to this change, you'd need to do the following to set up
    associations for composite primary key models:
    
    ```ruby
    class Order
      self.primary_key = [:shop_id, :id]
    
      has_many :order_agreements, primary_key :id
    end
    
    class OrderAgreement
      belongs_to :order, primary_key: :id
    end
    ```
    
    After this change, the `primary_key` option no longer needs to be
    specified:
    
    ```ruby
    class Order
      self.primary_key = [:shop_id, :id]
    
      has_many :order_agreements
    end
    
    class OrderAgreement
      belongs_to :order
    end
    ```
    2fdd1ad6
    Infer primary_key: :id on associations with composite primary key models
    Adrianna Chang authored
    Previously, a composite primary key model would need to specify
    either a `primary_key` or `query_constraints` option on its associations.
    Without it, a `CompositePrimaryKeyMismatchError` would be raised.
    
    Most of the time, the composite primary key includes the `:id` column,
    and associations already expect that `:id` is being used as the
    primary key for the association (and have a corresponding foreign_key
    column set on the associated model).
    
    Rather than requiring users to define `primary_key: :id` throughout
    an application with composite primary key associations, we can infer
    that the `:id` column should be used. Users can still override this
    behaviour by specifying a `primary_key:` or `query_constraints:` on
    the association.
    
    Note that, if the composite primary key for a model does _not_ include
    `:id`, Rails won't infer the primary key for any related associations,
    and users must still specify `query_constraints` or `primary_key`.
    
    Prior to this change, you'd need to do the following to set up
    associations for composite primary key models:
    
    ```ruby
    class Order
      self.primary_key = [:shop_id, :id]
    
      has_many :order_agreements, primary_key :id
    end
    
    class OrderAgreement
      belongs_to :order, primary_key: :id
    end
    ```
    
    After this change, the `primary_key` option no longer needs to be
    specified:
    
    ```ruby
    class Order
      self.primary_key = [:shop_id, :id]
    
      has_many :order_agreements
    end
    
    class OrderAgreement
      belongs_to :order
    end
    ```
Loading