metabase / toucan

A classy high-level Clojure library for defining application models and retrieving them from a DB
Eclipse Public License 1.0
570 stars 49 forks source link

insert! returns differently between models #32

Open hawarir opened 6 years ago

hawarir commented 6 years ago

Hi, so I have two models here, lets say one is Model A and the other one is Model B. When I insert! Model A, the function returns its identity, but when I insert! Model B, the returned value is nil.

The difference between this model is that Model A is using generated id from mysql, where Model B is using id composed in my application:

(defmodel B :b
  IModel
  (pre-insert [payload]
              (assoc payload
                     :id (.toString (java.util.UUID/randomUUID))))

=> (insert! A {:name "John Doe" :email "john@doe.com"}
#AInstance{:id 1 :name "John Doe" :email "john@doe.com"}

=> (insert! B {:name "John Doe" :email "john@doe.com"}
nil

Is this behavior expected?

camsaul commented 6 years ago

Hi @hawari17 unfortunately Toucan still makes assumptions that models have a primary key named :id which of course is not true for everybody. See issue #3.

If you have some spare cycles I would happily merge a PR to improve support for situations like the one you're running in to!