I have a tenant architecture. In a table “payment_methods” are the payment methods that can be both shared among all the tenants and be overwritten by each individual tenant. There’s “payment_name_id” that identifies a payment system by name, let’s say “paypal”.
-
when “tenant_id” field is nil, it’s shared among all the tenants, by default.
-
when “tenant_id” field is nil and there’s another record with “tenant_id = 123” and the same “payment_name_id”, it’s shared among all the tenants and that individual tenant with id = 123 has to use the record with tenant_id = 123 instead of the one with tenant_id = nil.
-
when “tenant_id = 123” and there’re no other records with tenant_id = nil for a given “payment_name_id”,a tenant with id 123, and only that tenant, may use such a payment method
How do I create a query in Ecto for this? I’d check if there’s a payment_method with “tenant_id” = “my_tenant_id” and payment_name_id, and if there’s, I’d use it, if not I’d fallback to the one with tenant_id = nil for a given payment_name_id, if it exists at all.
Do there have to be 2 separate queries necessarily? I know how to create 2, but is there a way to do it simpler, in a single query?