如何在Lobos中设置引用列值?

时间:2015-04-29 05:15:01

标签: clojure database-migration

使用Lobos,我的迁移看起来像:

(defmigration company-table
  (up [] (create
          (tbl :companies
               (text name))))
  (down [] (drop
            (table :companies))))

(defmigration document-table
  (up [] (create
          (tbl :documents
               (timestamp :date_of_event)
               (text :title)
               (text :name)
               (refer-to :companies))))
  (down [] (drop
            (table :documents))))

我想在文件s.t上设置参考。引用列名称是company_id,例如(refer-to :companies as :company_id)。我该怎么做?

1 个答案:

答案 0 :(得分:0)

documentation看起来不是很好,但有两种方法可以做到这一点:

  • 使用foreign-key功能
(defmigration document-table
  (up [] (create
          (table :documents
               (timestamp :date_of_event)
               (text :title)
               (text :name)
               (foreign-key :company-id :companies))))
;; or, if you need to specify options, you'd use something like this
               (foreign-key :company-id :companies :on-delete :cascade))))
  • 使用[:refer tname & options]选项。
(defmigration document-table
  (up [] (create
          (table :documents
               (timestamp :date_of_event)
               (text :title)
               (text :name)
               (integer :company-id [:refer companies]))))
;; or, if you need to specify options, you'd use something like this
               (integer :company-id [:refer companies :on-delete :cascade]))))

修改另外,除非您定义了自己的函数tbl,否则您可能意味着table

编辑2:文档here看起来好多了。