This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-01-02
Channels
- # babashka (117)
- # babashka-sci-dev (6)
- # beginners (34)
- # biff (2)
- # calva (7)
- # clj-kondo (27)
- # clojure (6)
- # clojure-dev (8)
- # clojure-europe (41)
- # clojure-israel (1)
- # clojure-nl (1)
- # clojure-norway (2)
- # clojure-uk (1)
- # clojurescript (22)
- # cursive (3)
- # datascript (12)
- # dev-tooling (4)
- # emacs (13)
- # hyperfiddle (60)
- # introduce-yourself (8)
- # joyride (9)
- # lsp (46)
- # malli (3)
- # mranderson (75)
- # off-topic (40)
- # pathom (9)
- # pedestal (4)
- # reagent (11)
- # reitit (18)
- # releases (2)
- # shadow-cljs (81)
- # squint (18)
Is there a way to upsert on a list? A simplified version of my schema is
(def conn (d/create-conn
{:location/name {:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity}
:location/goods {:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many}
:symbol {}
:price {}}))
and the problem I'm having is that when I update location (to reflect price changes), the new symbol/price entities are appended to the list instead of updated.An example of what I mean
(ns datascript-example
(:require [datascript.core :as d]))
(def conn (d/create-conn
{:location/name {:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity}
:location/goods {:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many}
:symbol {}
:price {}}))
(d/transact! conn
[{:location/name "New York City"
:location/goods [{:symbol "Cheese"
:price 10}
{:symbol "Chocolate"
:price 2}]}])
(ffirst (d/q '[:find (count ?e)
:where [?e]] @conn))
Everytime the transaction is run, the count increases by 2, when imo it should stay the same.symbol isn't unique because different locations can have the same goods in their list of goods, but the prices need to be different
I considered duplicating :location/name
on the goods and setting that to be unique but that duplication seems wrong to me
I'll need a unique attribute to update the price, then?
maybe a hash map
{:location/name "New York City"
:location/goods {"Cheese" {:price 10}}}
I did a quick test, and I can generate a unique attribute with [symbol location/name]
which seems to make the updates work as expected