Caribou: Unable to create a model that has a self reference!

59 views
Skip to first unread message

David Baldin

unread,
Feb 4, 2014, 4:11:06 AM2/4/14
to clo...@googlegroups.com
Hey!

i hope this is the right place to ask this question! Here it goes: I want to create a simple model with cariou that references itself to generate a tree-structure. Like this:

(defn category []
    (model/create :model {:name "category"
                        :fields [{:name "Label" :type "string"}
                                 {:name "related-categories" :type "link"
                                   :target-id (model/models :category :id)
                                   :reciprocal-name "relating-categories"}]}))


This results in a:

DEBUG :db insert into field values dependent = false, slug = 'relating-categories', position = 201, map = false, localized = false, name = 'relating-categories', locked = false, model-id = '', target-id = 22, uuid = '13776b59-7359-4412-850f-ffaa619665e0', type = 'link', updated-at = '2014-02-04 09:53:48.578', model-position = 201, link-id = 380, status-position = 139
Exception in thread "main" org.h2.jdbc.JdbcSQLException: NULL not allowed for column "MODEL_ID"; SQL statement:
INSERT INTO field (dependent,slug,position,map,localized,name,locked,model_id,target_id,uuid,type,updated_at,model_position,link_id,status_position) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) [23502-170]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
    at org.h2.message.DbException.get(DbException.java:169)
    at org.h2.message.DbException.get(DbException.java:146)
    at org.h2.table.Column.validateConvertUpdateSequence(Column.java:293)
...

Because the category to be reference is not yet available. Is there a way to create a model and afterwards update it with constraints (Like you would do in SQL)!?

Btw. naturally the other way around will dont work either:

(defn category []
    (model/create :model {:name "category"
                        :fields [{:name "Label" :type "string"}
                                 {:name "keywords" :type "string"}
                                 {:name "parent-category" :type "part"
                                   :target-id (model/models :category :id)
                                   :reciprocal-name "child-categories"}]}))


And the bidirectional (or in caribou-speech: "reciprocal") also not:

(defn category []
    (model/create :model {:name "category"
                        :fields [{:name "Label" :type "string"}
                                 {:name "keywords" :type "string"}
                                 {:name "parent-category" :type "link"
                                   :target-id (model/models :category :id)}]}))


Any hints? Btw. Caribou is really pragmatic, fast and clean, love it! NoSQL/Riak support would awesome too!

Thanks and regards,
David

David Baldin

unread,
Feb 4, 2014, 5:05:25 AM2/4/14
to clo...@googlegroups.com
... one step closer:

(defn category []
  ((model/create :model {:name "category"
                       :fields [{:name "Label" :type "string"}
                                {:name "keywords" :type "string"}
                                {:name "parent-id" :type "integer"}]})
    (db/add-reference :field :parent-id :category :destroy)))


now i have to touch the model-table somehow.

David Baldin

unread,
Feb 4, 2014, 5:23:37 AM2/4/14
to clo...@googlegroups.com
Well, source-code is the best documentation:

(defn category []
  ((model/create :model {:name "category"
                       :fields [{:name "Label" :type "string"}]})
    (let [category-model-id (model/models :category :id)]
      (model/update :model
category-model-id {:name "category"
                                          :fields [{:name "parent-category" :type "part"
                                                    :target-id prod-model-id
                                                    :reciprocal-name "sub-categories"}]}))))


Documentation of caribou.model/create and caribou.model/update describes it all!

Have a nice day!


Am Dienstag, 4. Februar 2014 10:11:06 UTC+1 schrieb David Baldin:
Reply all
Reply to author
Forward
0 new messages