Search code examples
schemadatomic

How to import :bigdec data into datomic


I have this schema:

[
  {:db/id #db/id[:db.part/db]
   :db/ident :atr/amount
   :db/valueType :db.type/bigdec
   :db/cardinality :db.cardinality/one
   :db/fulltext false
   :db.install/_attribute :db.part/db}

  {:db/id #db/id[:db.part/db]
   :db/ident :atr/clientId
   :db/valueType :db.type/string
   :db/cardinality :db.cardinality/one
   :db/fulltext true
   :db/doc "The client who owns this atr"
   :db.install/_attribute :db.part/db}

  {:db/id #db/id[:db.part/db]
   :db/ident :atr/currency
   :db/valueType :db.type/string
   :db/cardinality :db.cardinality/one
   :db/fulltext true
   :db.install/_attribute :db.part/db}

  {:db/id #db/id[:db.part/db]
   :db/ident :atr/valueDate
   :db/valueType :db.type/instant
   :db/cardinality :db.cardinality/one
   :db/fulltext true
   :db.install/_attribute :db.part/db}

  {:db/id #db/id[:db.part/db]
   :db/ident :community/reference
   :db/valueType :db.type/string
   :db/cardinality :db.cardinality/one
   :db.install/_attribute :db.part/db}
]

I try to insert this data:

[
    {:db/id 1, :atr/amount 10, :atr/currency "USD", :atr/clientId "clientId", :atr/valueDate "2014-02-14"}
]

I get this error message:

Caused by: java.lang.IllegalArgumentException: :db.error/wrong-type-for-attribute Value 10 is not a valid :bigdec for attribute :atr/amount

How do I describe bigdec data for import into datomic?


Solution

  • You need to use the appropriate data literal to express the value in this case (using M to indicate BigDecimal):

    (def foo-tx
      [
        {:db/id (d/tempid :db.part/user)
         :atr/amount 10M
         :atr/currency "USD"
         :atr/clientId "clientId"
         :atr/valueDate #inst "1985-04-12T23:20:50.52Z"} ;;)(new java.util.Date)}
       ])
    

    Or otherwise create the correct value type (as with date below):

    (def foo-tx
      [
        {:db/id (d/tempid :db.part/user)
         :atr/amount 10M
         :atr/currency "USD"
         :atr/clientId "clientId"
         :atr/valueDate (new java.util.Date)}
       ])
    

    Both of these examples will transact correctly against your schema.