1

I have the following code:

(defentity users
  (database korma-db)
  (has-many tags))

(defentity tags
  (database korma-db)
  (belongs-to users))

(-> (select* users)
    (with tags)
    (fields :address)
    (where {:id 1})
    (as-sql))

and it generates the following sql:

SELECT "users"."address" FROM "users" WHERE ("users"."id" = ?)

While I would expect it to include a join to the tags table, by merit of having the with macro applied. Clearly this isn't the case, but executing it will produce an empty :tags key in the single returned record.

Am I missing something here?

Ryan Jenkins
  • 878
  • 8
  • 16
  • 1
    did you try without `fields`? – edbond May 27 '14 at 12:14
  • 1
    Just did, and that fixed it. I didn't expect korma to remove the join if I wasn't selecting any of the tag fields, which is good I suppose, just caught me by surprise. Also odd that it still adds the :tags key but leaves its value empty. But in any case, that did it. Thank you. – Ryan Jenkins May 27 '14 at 19:24
  • 1
    Yeah, Korma's `with` joins will fail if you don't select the key columns in your `fields`. I haven't found a way of excluding them and using `with`, but there's another way in a similar question I asked: https://stackoverflow.com/questions/21242097/select-no-fields-from-table-in-korma – Conan Sep 01 '14 at 15:54

1 Answers1

0

did you create the actual referential constraint on the database? I think I had the same issue once and I fixed it by creating a foreign key when defining the field. i.e. in PostgreSQL

CREATE TABLE tags (
...
 users_id INTEGER REFERENCES users(id),
)
Nico Balestra
  • 256
  • 3
  • 12