datomic

Ask questions on the official Q&A site at https://ask.datomic.com!
ccortes 2021-04-24T11:42:24.379400Z

I had the same problem when I was trying to build a website with clojure and datomic. This https://github.com/milgra/tutorials/blob/master/full-stack-web-development-with-clojure-and-datomic.md helped me a lot.

2021-04-24T12:56:00.379700Z

Ah, this is great, thank you! This reminds me a little bit of the "Immutable Stack" series: https://youtu.be/QrSnTIHotZE

Luan 2021-04-24T21:06:01.383300Z

Hello everyone, I have two entities, user and address, I was trying to use tuples to restrict a user to have only addresses with different types, but when I transact one user with address it doesn't put the address type too ":user/id+addrtype [#uuid "c8335aeb-686f-438b-bbb7-06691ac81c69" nil]". This is my tuple:`{:db/ident :user/id+addrtype :db/tupleAttrs [:user/id :address/type]}` . Is that correct or is there any alternative I can use for this problem?

Joe Lane 2021-04-24T21:12:59.383600Z

What is the tx-data and full schema @cbluan?

Luan 2021-04-24T21:26:32.384200Z

It works when I use :address/type along with :user/id and the same data nested

Joe Lane 2021-04-24T21:27:15.384400Z

I can't help unless I see the schema and tx-data

Luan 2021-04-24T21:34:48.384600Z

Sorry, I misclicked, this is the schema:

{:db/id #db/id [:db.part/db]
 :db/ident :address/type
 :db/valueType :db.type/string
 :db/unique :db.unique/identity
 :db/cardinality :db.cardinality/one
 :db.install/_attribute :db.part/db}
 {:db/id #db/id [:db.part/db]
  :db/ident :address/street
  :db/valueType :db.type/string
  :db/cardinality :db.cardinality/one
  :db.install/_attribute :db.part/db}
 {:db/id #db/id [:db.part/db]
  :db/ident :user/id
  :db/valueType :db.type/uuid
  :db/unique :db.unique/identity
  :db/cardinality :db.cardinality/one
  :db.install/_attribute :db.part/db}
 {:db/id #db/id [:db.part/db]
  :db/ident :user/name
  :db/valueType :db.type/string
  :db/cardinality :db.cardinality/one
  :db.install/_attribute :db.part/db}
 {:db/id #db/id [:db.part/db]
  :db/ident :user/address
  :db/valueType :db.type/ref
  :db/cardinality :db.cardinality/many
  :db.install/_attribute :db.part/db}
 {:db/ident :user/userid+type
  :db/valueType :db.type/tuple
  :db/tupleAttrs [:user/id :address/type]
  :db/cardinality :db.cardinality/one
  :db/unique :db.unique/identity
  :db.install/_attribute :db.part/db}
And the tx:
{:user/id      #uuid "c8335aeb-686f-438b-bbb7-06691ac81c69"
 :address/type "Home" ;; it it works when I add it here
 :user/name    "Chris"
 :user/address {:db/id (d/tempid :db.part/user)
                :address/type "Home"
                :address/street "St. 222"}}

Joe Lane 2021-04-24T21:40:11.384800Z

Validate an experiment for me: I really do want you to use "Foo" here for the address type first transact:

{:db/id (d/tempid :db.part/user)
 :address/type "Foo"
 :address/street "St. 222"}
Then, in a second transaction, transact:
{:address/type "Foo"
 :address/street "St. 123"}
Then: (d/pull a-fresh-db '[*] [:address/type "foo"]) What do you get back?

Luan 2021-04-24T21:50:26.385Z

I used "Foo" instead, using only these two transactions I get: {:db/id 17592186045418, :address/type "Foo", :address/street "St. 123", :user/userid+type [nil "Foo"]}

Joe Lane 2021-04-24T22:02:39.385300Z

So it appears that in your schema :address/type is unique like :user/id , and therefore, whenever a second address with {:address/type "Home", :address/street "Route 66"} is added to the system it would overwrite the first user's address. Your users might find this problematic 🙂

Joe Lane 2021-04-24T22:03:37.385500Z

The reason the tuple "Works" when you attach it to the user is because you have what is known as a "composite tuple", which can only read attributes from the same entity, not nested ones.

Joe Lane 2021-04-24T22:06:53.385700Z

How many :address/type's you expect to have in your application? If the answer is "A small number" you might benefit from creating some new attributes instead of an address entity. Example:

{:user/id #uuid "123..."
 :user/name "Chris"
 :address.home/street "St. 222"
 :address.billing/street "st. 123"
 :address.work/street "Route 66"}

Joe Lane 2021-04-24T22:08:36.385900Z

Schema would be something like:

{:db/id #db/id [:db.part/db]
 :db/ident :address.home/street
 :db/valueType :db.type/string
 :db/cardinality :db.cardinality/one
 :db.install/_attribute :db.part/db}
{:db/id #db/id [:db.part/db]
 :db/ident :address.work/street
 :db/valueType :db.type/string
 :db/cardinality :db.cardinality/one
 :db.install/_attribute :db.part/db}
{:db/id #db/id [:db.part/db]
 :db/ident :address.billing/street
 :db/valueType :db.type/string
 :db/cardinality :db.cardinality/one
 :db.install/_attribute :db.part/db}

Joe Lane 2021-04-24T22:09:49.386100Z

Then, a user can only have 1 address of each "type"

Luan 2021-04-24T22:23:23.386300Z

I was trying to to this composite tuple, but I didn't know it doesn't work on nested entities 🙂 And I also was trying to model it to support another entity like Company to have an address, so I would just ref to it. I will think about this solution, thank you 🙂 .

Joe Lane 2021-04-24T22:34:09.386600Z

FWIW @cbluan, companies can use those same attributes:

{:db/id #db/id [:db.part/db]
 :db/ident :address.mailing/street
 :db/valueType :db.type/string
 :db/cardinality :db.cardinality/one
 :db.install/_attribute :db.part/db}

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

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

{:db/id #db/id [:db.part/db]
 :db/ident :company/employees
 :db/valueType :db.type/ref
 :db/cardinality :db.cardinality/many
 :db.install/_attribute :db.part/db}
Then:
{:company/name "Acme
 :favorite/color "Blue"
 :address.mailing/street "Route 66"
 :address.billing/street "123 Nowhere St."
 :company/employees [

{:user/id #uuid "123..."
 :user/name "Chris"
 :favorite/color "Green"
 :address.home/street "St. 222"
 :address.billing/street "st. 123"
 :address.work/street "Route 66"}

]}
Don't think in rectangles columns, instead, try to think in sets of attributes

1👍1🙂