datomic

Ask questions on the official Q&A site at https://ask.datomic.com!
oxalorg (Mitesh) 2020-12-02T13:36:53.039700Z

I'm trying to add a child entity with a ref to a parent entity using a lookup-ref

(d/transact conn {:comments/title "foo" :comments/article [:article/title "bar"]})
But I have incomplete data of B. So sometimes there is no article called "bar". This gives me a :db.error/not-an-entity error. How would I go about writing this query so that if "bar" does not exist then only add the child "foo" but keep it's :child/parent attribute as empty?

plexus 2020-12-02T14:48:43.041200Z

I'm setting up Datomic Analytics for a client, and am running into an issue with BigDecimal

{:type "java.lang.ArithmeticException",
   :message "Rounding necessary",
   :suppressed [],
   :stack
   ["java.base/java.math.BigDecimal.commonNeedIncrement(BigDecimal.java:4529)"
    "java.base/java.math.BigDecimal.needIncrement(BigDecimal.java:4585)"
    "java.base/java.math.BigDecimal.divideAndRound(BigDecimal.java:4493)"
    "java.base/java.math.BigDecimal.setScale(BigDecimal.java:2799)"
    "java.base/java.math.BigDecimal.setScale(BigDecimal.java:2732)"
    "io.prestosql.spi.type.Decimals.encodeScaledValue(Decimals.java:172)"
    "io.prestosql.spi.type.Decimals.encodeScaledValue(Decimals.java:166)"
    "datomic.presto$create_connector$reify$reify$reify$reify__2435.getSlice(presto.clj:348)"

plexus 2020-12-02T14:49:08.041400Z

could this be a bug in the connector?

ghadi 2020-12-02T16:17:41.042100Z

@plexus I seem to recall the latest release notes having something about that

2020-12-02T17:01:03.043600Z

are there diagramming techniques or approaches that are more suited to datomic than traditional sql rectangles - "an entity relationship diagram"?

2020-12-02T17:01:28.044Z

like drawing nodes and named edges, maybe?

alexmiller 2020-12-02T17:03:34.044200Z

erds are still pretty useful as long as you keep in mind that entity "types" are a fiction of your model, not a constraint in Datomic

🙏 1
alexmiller 2020-12-02T17:04:49.044500Z

for example, here's a Datomic schema Rich made for Codeq https://github.s3.amazonaws.com/downloads/Datomic/codeq/codeq.pdf

2020-12-02T17:07:00.044800Z

thank you! looks like it leaves off the entity names - which, as you say, are a fiction.

alexmiller 2020-12-02T17:07:07.045Z

in any of those rectangles you have a bunch of attributes that are used together (but just keep in mind that the box doesn't really exist). the lines (attributes) are the important part

alexmiller 2020-12-02T17:08:10.045300Z

I think yellow boxes there indicate uniqueness

alexmiller 2020-12-02T17:08:57.045500Z

there is one for the mbrainz sample at https://github.com/Datomic/mbrainz-sample

alexmiller 2020-12-02T17:09:44.045800Z

that one has a stronger sense of entity type (but really is just a shared namespace for the attrs)

alexmiller 2020-12-02T17:11:36.046Z

these are both good examples of what Datomic devs do - different variants may emphasize different aspects of the schema (type, cardinality, uniqueness, component)

2020-12-02T17:12:45.046200Z

this is very helpful, i appreciate it.

2020-12-02T17:13:40.046400Z

good to know that this is the diagramming style favored by other datomic developers.

pithyless 2020-12-02T17:15:43.046600Z

@mitesh 3 common ways to go about this: 1. Change the [:article/title "bar"] to {:article/title "bar"}. Assuming :article/title is unique and :comments/article is a to-one relationship, this won't throw the error, but will either use the existing article or create a new article (with just that attribute). 2. If you don't want to create a new (possibly incomplete article), you need to query the db for existence before doing the transact and modify the transaction accordingly. 3. If you're querying the DB to check for existence and then doing a transact, this may lead to a race-condition. If this is important to avoid, you can instead create a custom transaction function and move the check for existence within the transactor. This will guarantee that you don't have a race-condition (your transaction function will check for existence of the article and then transform the transaction data accordingly before committing).

🎉 1
Wojtek 2020-12-02T20:31:27.049200Z

I have few really slow datomic queries - how could I improve them?

2020-12-02T22:42:30.049700Z

The usual answer would be: do you have the order of your clauses correct

Wojtek 2020-12-02T23:54:24.050200Z

thank you! but I have already tried to reorder my clauses without improvement 😞