composite-primary-keys/composite_primary_keys

Belong To With Different Keys not generating correct SQL

Closed this issue · 1 comments

Hi All,

I have the following models in Rails 5.2.x:

class Order < ApplicationRecord
  belongs_to :customer, optional: true,  foreign_key: [:customer_id, :shop_id]
  belongs_to :shop
end
class Customer < ApplicationRecord
  self.table_name  = :partitioned_customers
  self.primary_key = [:id, :shop_id]
  has_many :orders
end

When I run:

ShopifyShop.all.last.orders.includes(:customer).first

I get the following query which doesn't make sense to me:

SELECT "partitioned_customers".* FROM "partitioned_customers" WHERE "partitioned_customers"."id" IS NULL AND "partitioned_customers"."shop_id" = 'bcc8be14-5c62-....'

partitioned_customers.id should not be checked against null. Can anyone point me in the right direction?

Thanks for the help

My mistake I fixed it