     (checkp (integer? model_id) "id" (format "id is required parameter when filter mode is '%s'" (name f)))
     (case f
       :database (read-check Database model_id)
-      :table    (read-check Database (sel :one :field [Table :db_id] :id model_id))))
+      :table    (read-check Database (db/select-one-field :db_id Table, :id model_id))))
   (cards-for-filter-option f model_id label))
 
 (defendpoint POST "/"

(defn- cards:database
  "Return all `Cards` belonging to `Database` with DATABASE-ID."
  [database-id]
  (sel :many Card (k/order :name :ASC), :database_id database-id, :archived false))

(defn- cards:table
  "Return all `Cards` belonging to `Table` with TABLE-ID."
  [table-id]
  (sel :many Card (k/order :name :ASC), :table_id table-id, :archived false))

(defn- cards-with-ids
  "Return unarchived `Cards` with CARD-IDS.
   Make sure cards are returned in the same order as CARD-IDS`; `[in card-ids]` won't preserve the order."
  [card-ids]
  {:pre [(every? integer? card-ids)]}
  (let [card-id->card (sel :many :field->obj [Card :id], :id [in card-ids], :archived false)]
    (filter identity (map card-id->card card-ids))))

(defn- cards:recent
  "Return the 10 `Cards` most recently viewed by the current user, sorted by how recently they were viewed."
  []
  (cards-with-ids (map :model_id (k/select ViewLog
                                   (k/aggregate (max :timestamp) :max)
                                   (k/group :model_id)
                                   (k/fields :model_id)
                                   (k/where {:model (kx/literal "card"), :user_id *current-user-id*})
                                   (k/order :max :DESC)
                                   (k/limit 10)))))

(defn- cards:popular
  "All `Cards`, sorted by popularity (the total number of times they are viewed in `ViewLogs`).
   (yes, this isn't actually filtering anything, but for the sake of simplicitiy it is included amongst the filter options for the time being)."
  []
  (cards-with-ids (map :model_id (k/select ViewLog
                                   (k/aggregate (count (k/raw "*")) :count)
                                   (k/group :model_id)
                                   (k/fields :model_id)
                                   (k/where {:model (kx/literal "card")})
                                   (k/order :count :DESC)))))

(defn- cards:archived
  "`Cards` that have been archived."
  []
  (sel :many Card, :archived true, (k/order :name :ASC)))

(def ^:private filter-option->fn
  "Functions that should be used to return cards for a given filter option. These functions are all be called with `model-id` as the sole paramenter;

                             [label :refer [Label]]
                             [table :refer [Table]]
                             [view-log :refer [ViewLog]])
            [metabase.util :as u]))

(defn- hydrate-labels
  "Efficiently hydrate the `Labels` for a large collection of `Cards`."
  [cards]
  (let [card-labels          (db/select [CardLabel :card_id :label_id])
        label-id->label      (when (seq card-labels)
                               (db/select-id->obj Label :id [:in (map :label_id card-labels)]))
        card-id->card-labels (group-by :card_id card-labels)]
    (for [card cards]
      (assoc card :labels (for [card-label (card-id->card-labels (:id card))] ; TODO - do these need to be sorted ?
