 	old-rhs-indeces (second old-indeces)
 	lhs-indeces (first old-indeces)
 	lhses (lhses-get lhs-indeces i fk)
-	old-joins (mapcat (fn [lhs] (derive-joins old-rhs-indeces lhs-fk-keys lhs join-fn)) lhses)
+	old-joins (mapcat
+		   (fn [lhs] (vals (reduce conj {} (derive-joins old-rhs-indeces lhs-fk-keys lhs join-fn))))
+		   lhses)
 	new-indeces (rhs-dissoc old-indeces i rhs-pk-key fk rhs)
 	new-rhs-indeces (second new-indeces)
-	new-joins (mapcat (fn [lhs] (derive-joins new-rhs-indeces lhs-fk-keys lhs join-fn)) lhses)
+	new-joins (mapcat
+		   (fn [lhs] (vals (reduce conj {} (derive-joins new-rhs-indeces lhs-fk-keys lhs join-fn))))
+		   lhses)
 	joins (remove (fn [i] (contains? (apply hash-set new-joins) i)) old-joins)
 	]
     [new-indeces (fn [_] (on-deletes joined-model joins))]

	lhs-indeces (first old-indeces)
	lhses (lhses-get lhs-indeces i fk)
	rhs-indeces (second new-indeces)
	joins (mapcat (fn [lhs] (derive-joins rhs-indeces lhs-fk-keys lhs join-fn)) lhses)
	]
    [new-indeces (fn [_] (on-upserts joined-model joins))]
    ))



(defn- derive-joins [rhs-indeces lhs-fk-keys lhs join-fn]
  (map
   (fn [rhses] (let [join (apply join-fn lhs rhses)] [(:name join) join])) ;TODO - pass in join-pk
   (permute [[]] (rhses-get rhs-indeces lhs-fk-keys lhs))))

;;--------------------------------------------------------------------------------

(defn- lhs-upsert [old-indeces lhs-pk-key lhs-fk-keys lhs join-fn joined-model]
  (let [new-indeces (lhs-assoc old-indeces lhs-pk-key lhs-fk-keys lhs)
	join-pairs (derive-joins (second new-indeces) lhs-fk-keys lhs join-fn)
	joins (vals (reduce conj {} join-pairs))]
    [new-indeces (fn [_] (on-upserts joined-model joins))]))

(defn- lhs-delete [old-indeces lhs-pk-key lhs-fk-keys lhs join-fn joined-model]
  (let [new-indeces (lhs-dissoc old-indeces lhs-pk-key lhs-fk-keys lhs)
	join-pairs (derive-joins (second new-indeces) lhs-fk-keys lhs join-fn)
	joins (vals (reduce conj {} join-pairs))]
    [new-indeces (fn [_] (on-deletes joined-model joins))]))

;;; TODO
