Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

join queries with mito #103

Closed
rajasegar opened this issue Jul 20, 2021 · 3 comments
Closed

join queries with mito #103

rajasegar opened this issue Jul 20, 2021 · 3 comments

Comments

@rajasegar
Copy link

I have a table schemas like the below:

(mito:deftable warehouses ()
  ((location :col-type (:varchar 50))
   (capacity :col-type (:integer))))

(mito:deftable boxes ()
  ((contents :col-type (:varchar 10))
   (value :col-type (:integer))
   (warehouse :col-type warehouses :references warehouses)))

How can I fetch the list of boxes along with their warehouse names, it is pretty straigthforward with sql or sxql, but I want to know how can we do this with mito?

select contents, value, location from boxes inner join warehouses where boxes.warehouse = warehouse.id
@vindarel
Copy link
Contributor


test code:

(mito:connect-toplevel :sqlite3 :database-name "mito-example.db")

(mapc #'mito:execute-sql (mapcan #'mito:table-definition '(WAREHOUSES boxes)))

;; data:
(mito:create-dao 'boxes :contents "foo" :value 9 :warehouse (mito:create-dao 'warehouses :location "here" :capacity 2))
;; #<BOXES {10060E3353}>

(inspect *)

The object is a STANDARD-OBJECT of type BOXES.
0. CREATED-AT: @2021-07-20T11:57:21.775323+02:00
1. UPDATED-AT: @2021-07-20T11:57:21.775323+02:00
2. SYNCED: T
3. ID: 1
4. CONTENTS: "foo"
5. VALUE: 9
6. WAREHOUSE: #<WAREHOUSES {1005942393}>
7. WAREHOUSE-ID: "unbound"

Normally there is built-in accessor to warehouse but it is broken (#79)

You can add an accessor yourself.

…
   (warehouse :col-type warehouses :references warehouses
              :accessor warehouse)))  ;; <--- added
…

(warehouse **)
;; #<WAREHOUSES {1005942393}>

Was that your goal or it was to only get a warehouse.location data?

@rajasegar
Copy link
Author

@vindarel Thanks, and yes that was my goal, I kinda figured it out later to use includes

(mito:select-dao 'boxes (mito:includes 'warehouses))

This works for my use case

@daninus14
Copy link
Contributor

Can you please close the issue if there is nothing else needed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants