diff --git a/src/datascript/db.cljc b/src/datascript/db.cljc index 2da26d5e..b02a6c34 100644 --- a/src/datascript/db.cljc +++ b/src/datascript/db.cljc @@ -1285,14 +1285,16 @@ (let [err-msg (str "Schema with attribute " v-ident " does not exist") err-map {:error :retract/schema :attribute v-ident}] (throw (ex-info err-msg err-map))) - (update-in db [:schema] #(dissoc % v-ident))) - (let [e-ident (:v (first (-seek-datoms db :eavt e :db/ident nil nil)))] - (if (schema e-ident) - (update-in db [:schema e-ident] #(dissoc % a-ident)) - (let [err-msg (str "Schema with db/ident " e-ident " does not exist") - err-map {:error :retract/schema :entity-id e :attribute a :value e}] - (throw #?(:clj (ex-info err-msg err-map) - :cljs (ex-info err-msg err-map))))))))) + (-> db + (assoc-in [:schema e] (dissoc (schema v-ident) a-ident)) + (update-in [:schema] #(dissoc % v-ident)))) + (if-let [schema-entry (schema e)] + (if (schema schema-entry) + (update-in db [:schema schema-entry] #(dissoc % a-ident)) + (update-in db [:schema e] #(dissoc % a-ident v-ident))) + (let [err-msg (str "Schema with entity id " e " does not exist") + err-map {:error :retract/schema :entity-id e :attribute a :value e}] + (throw (ex-info err-msg err-map))))))) (defn get-schema [db] (or (:schema db) {})) @@ -1305,9 +1307,14 @@ a-ident a v-ident v] (if (= a-ident :db/ident) - (assoc-in db [:schema v-ident a-ident] v-ident) - (let [e-ident (:v (first (-seek-datoms db :eavt e :db/ident nil nil)))] - (assoc-in db [:schema e-ident a-ident] v-ident))))) + (-> db + (assoc-in [:schema v-ident] (merge (or (schema e) {}) (hash-map a-ident v-ident))) + (assoc-in [:schema e] v-ident)) + (if-let [schema-entry (schema e)] + (if (schema schema-entry) + (assoc-in db [:schema schema-entry a-ident] v-ident) + (assoc-in db [:schema e a-ident] v-ident)) + (assoc-in db [:schema e] (hash-map a-ident v-ident)))))) (defn update-rschema [db] (assoc db :rschema (rschema (merge implicit-schema (get-schema db)))))