4

我让自己成为一个 facebook/twitter 组合只是为了好玩,但我不能 100% 确定 JDBC 的某些语义。我不确定如何将字段设置为外键或如何限制 varchar 字段中的字符数。

我的代码(带有注释以显示我需要帮助的地方)如下

(ns twitface.models.migration
  (:require [clojure.java.jdbc :as sql]))

(defn create-tables []
  (sql/with-connection (System/getenv "DATABASE_URL")
    (do 
      (sql/create-table :users
                        [:id :serial "PRIMARY KEY"]
                        [:email :varchar "NOT NULL"]
                        [:password :varchar "NOT_NULL"]
                        [:full-name :varchar "NOT NULL"]
                        [:pref-name :varchar]
                        [:created_at :timestamp "NOT NULL" "DEFAULT CURRENT_TIMESTAMP"])
      (sql/create-table :relations
                        [:id :serial "PRIMARY KEY"]
                        [:relating-user :numeric] ; foreign key
                        [:related-user :numeric]  ; foreign key
                        [:status :varchar])
      (sql/create-table :posts
                        [:id :serial "PRIMARY KEY"]
                        [:user :numeric "NOT NULL"]    ; foreign key
                        [:destination-user :numeric "NOT NULL"] ; foreign key
                        [:message :varchar "NOT NULL"] ; 180 char limit
                        [:created_at :timestamp "NOT NULL" "DEFAULT CURRENT_TIMESTAMP"])
      (sql/create-table :comments
                        [:id :serial "PRIMARY KEY"]
                        [:user :numeric "NOT NULL"]    ; foreign key
                        [:message :varchar "NOT NULL"] ; 180 char limit
                        [:created_at :timestamp "NOT NULL" "DEFAULT CURRENT_TIMESTAMP"]))))

任何帮助,将不胜感激!提前致谢!

4

2 回答 2

3

当您找不到文档时,请查看项目中包含的测试,如果这没有帮助,请查看源代码 - 使用开源的最佳方式。我尝试查看测试,发现您可以使用VARCHAR(180)而不是使用:varchar来指定列的限制,同样您可以使用普通 SQL 构造来指定外键

于 2012-05-28T06:13:42.440 回答
3

对于外键,references用作 sql:

(defn create-tables []
  (sql/with-connection (System/getenv "DATABASE_URL")
    (do
      (sql/create-table :users
                        [:id :serial "PRIMARY KEY"]
                        [:email "varchar(50)" "NOT NULL"]
                        [:password "varchar(30)" "NOT NULL"])
      (sql/create-table :relations
                        [:id :serial "PRIMARY KEY"]
                        [:related_user :serial "references users (id)"]
                        [:status "varchar(10)"]))))
于 2012-05-28T08:02:14.713 回答