specql icon indicating copy to clipboard operation
specql copied to clipboard

Multiple joins from one table to other

Open jaaqo opened this issue 7 years ago • 1 comments

In this case we have generic translation table that is used for string translations.

+--------------+         +-------------------+
|              | 1     * |                   |
| translation  +---------+ translation_value |
|              |         |                   |
+---+-----+----+         +-------------------+
    |     |
    | 1   | 1
    |     |
    | 1   | 1
    |     |
+---+-----+--------+
|                  |
| theme            |
|                  |
+------------------+
|                  |
| name_t_id        |
|                  |
| description_t_id |
|                  |
+------------------+

Defining tables:

(specql/define-tables db
  ["translation_value" :translation_value/translation_values]

  ["translation" :translation/translations
   {:translation/values (rel/has-many :translation/id
                                      :translation_value/translation_values
                                      :translation_value/translation_id)}]

  ["theme" :theme/themes
   {:theme/name (rel/has-one :theme/name_t_id
                             :translation/translations
                             :translation/id)
    :theme/description (rel/has-one :theme/description_t_id
                                    :translation/translations
                                    :translation/id)}])

Now querying:

;; translation.clj
(ns translation)

(def translation-keys
  #{[::values #{:translation_value/value
                :translation_value/id
                :translation_value/language}]})

;; theme.clj
(ns theme
  (:require [translation :as translation]
            [specql.core :as specql]))

(def theme-keys
  #{::id
    [::name translation/translation-keys]
    [::description translation/translation-keys]})

;; query for all themes
(specql/fetch
       db
       ::themes
       theme-keys
       {})

This query returns correctly themes and their info on first level.

Joins for name and description however do not.

Example data had two rows in translation_value table for all translation entries. So the expected row count for each is 2.

When selecting either key alone row count is correct and data is valid.

When both are in the query keys for :theme/name it seems only 1 row was returned and :theme/description had 4 (2*2 rows)

Have you run into this issue? If so how did you solve it? How should I solve it?

jaaqo avatar Sep 28 '17 15:09 jaaqo

Thanks for the bug report. I'll add a test-case for that so that it gets fixed in the next version.

Unfortunately, for now you may need to do 2 queries.

tatut avatar Oct 03 '17 14:10 tatut