   "Mark foreign-key `Fields` for TABLE as `special_type = fk` if they don't already have a `special_type`."
   {:arglists '([korma-table table])}
   [korma-table {database :db table-name :name table-id :id}]
-  (let [fks            (table-fks @database table-name)
-        fk-name->id    (sel :many :field->id [Field :name] :table_id table-id :special_type nil :name [in (map :fk-column-name fks)])
-        table-name->id (sel :many :field->id [Table :name] :name [in (map :dest-table-name fks)])]
-    (->> fks
-         (map (fn [{:keys [fk-column-name dest-column-name dest-table-name]}]
-                (when-let [fk-column-id (fk-name->id fk-column-name)]
-                  (when-let [dest-table-id (table-name->id dest-table-name)]
-                    (when-let [dest-column-id (sel :one :id Field :table_id dest-table-id :name dest-column-name)]
-                      (log/info (format "Marking foreign key '%s.%s' -> '%s.%s'." table-name fk-column-name dest-table-name dest-column-name))
-                      (ins ForeignKey
-                        :origin_id fk-column-id
-                        :destination_id dest-column-id
-                        :relationship (determine-fk-type korma-table fk-column-name))
-                      (upd Field fk-column-id :special_type :fk))))))
-         dorun)))
+  (try
+    (let [fks            (table-fks @database table-name)
+          fk-name->id    (sel :many :field->id [Field :name] :table_id table-id :special_type nil :name [in (map :fk-column-name fks)])
+          table-name->id (sel :many :field->id [Table :name] :name [in (map :dest-table-name fks)])]
+      (->> fks
+           (map (fn [{:keys [fk-column-name dest-column-name dest-table-name]}]
+                  (when-let [fk-column-id (fk-name->id fk-column-name)]
+                    (when-let [dest-table-id (table-name->id dest-table-name)]
+                      (when-let [dest-column-id (sel :one :id Field :table_id dest-table-id :name dest-column-name)]
+                        (log/info (format "Marking foreign key '%s.%s' -> '%s.%s'." table-name fk-column-name dest-table-name dest-column-name))
+                        (ins ForeignKey
+                          :origin_id fk-column-id
+                          :destination_id dest-column-id
+                          :relationship (determine-fk-type korma-table fk-column-name))
+                        (upd Field fk-column-id :special_type :fk))))))
+           dorun))
+    (catch Throwable e
+      (log/error "Caught exception in set-table-fks-if-needed!:" e))))
 
 
 ;; ### Check for Low Cardinality

   This is only done for textual fields, i.e. ones with `special_type` of `:CharField` or `:TextField`."
  {:arglists '([korma-table field])}
  [korma-table {base-type :base_type, field-id :id, preview-display :preview_display, :as field}]
  (when (and preview-display                                 ; if field is already preview_display = false, no need to check again since there is no case
             (contains? #{:CharField :TextField} base-type)) ; where we'd end up changing it.
    (let [avg-len (field-avg-length korma-table field)]
      (when (> avg-len average-length-no-preview-threshold)
        (log/info (format "Field '%s.%s' has an average length of %d. Not displaying it in previews." (:table korma-table) (:name field) avg-len))
        (upd Field field-id :preview_display false)))))


;; ### Check for URLs

  (int (if *sql-string-length-fn*
         ;; If *sql-string-length-fn* is bound we can use just return AVG(LENGTH-FN(field))
         (-> korma-table
             (select (aggregate (avg (sqlfn* *sql-string-length-fn*
                                             (raw (format "CAST(\"%s\" AS TEXT)" (name field-name)))))
                                :len))
             first
             :len)
         ;; Otherwise we'll have to select *all* values of the Field and sum their counts in Clojure-land
