Replies: 2 comments 4 replies
-
|
Hi, I meant to tell you defining is not an issue, see the repo, I justed pushed an example but here it is anyways for others to benefit (ql:quickload "mito")
(setf mito:*auto-migration-mode* T)
(mito:connect-toplevel :sqlite3
:database-name ":memory:")
(defclass user ()
((name :col-type (or :null :text)))
(:metaclass mito:dao-table-class))
(mito:ensure-table-exists 'user)
(defclass book ()
((name :col-type (or :null :text)))
(:metaclass mito:dao-table-class))
(defclass user-book ()
((user :col-type (or :null user))
(book :col-type (or :null book)))
(:metaclass mito:dao-table-class))
(mito:ensure-table-exists 'book)
(mito:ensure-table-exists 'user-book)
(defvar *robert* (mito:insert-dao (make-instance 'user :name "robert ludlum")))
(defvar *supremacy* (mito:insert-dao (make-instance 'book :name "bourne supremacy")))
(defvar *ultimatum* (mito:insert-dao (make-instance 'book :name "bourne ultimatum")))
(defvar *r-s* (mito:insert-dao (make-instance 'user-book :user *robert* :book *supremacy*)))
(defvar *r-u* (mito:insert-dao (make-instance 'user-book :user *robert* :book *ultimatum*)))and here's the output in the REPL with a many-to-one relationship. Mito doesn't support multiple joins though see here #128 CL-USER> *robert*
#<USER {10064D0D43}>
CL-USER> (inspect *)
The object is a STANDARD-OBJECT of type USER.
0. CREATED-AT: @2024-11-05T01:13:14.968071+02:00
1. UPDATED-AT: @2024-11-05T01:13:14.968071+02:00
2. SYNCED: T
3. ID: 1
4. NAME: "robert ludlum"
> q
; No values
CL-USER> *r-u*
#<USER-BOOK {10063B60D3}>
CL-USER> (inspect *)
The object is a STANDARD-OBJECT of type USER-BOOK.
0. CREATED-AT: @2024-11-05T01:13:55.738294+02:00
1. UPDATED-AT: @2024-11-05T01:13:55.738294+02:00
2. SYNCED: T
3. ID: 2
4. USER: #<USER {1008328003}>
5. USER-ID: #<unbound slot>
6. BOOK: #<BOOK {100833E623}>
7. BOOK-ID: #<unbound slot>
> q
; No values
CL-USER> (mito:select-dao 'user (includes 'user-book))
; in: MITO.DAO:SELECT-DAO 'USER
; (INCLUDES 'USER-BOOK)
;
; caught STYLE-WARNING:
; undefined function: COMMON-LISP-USER::INCLUDES
;
; compilation unit finished
; Undefined function:
; INCLUDES
; caught 1 STYLE-WARNING condition
; Debugger entered on #<UNDEFINED-FUNCTION INCLUDES {100556E883}>
[1] CL-USER>
; Evaluation aborted on #<UNDEFINED-FUNCTION INCLUDES {100556E883}>
CL-USER> (mito:select-dao 'user (mito:includes 'user-book))
; Debugger entered on #<SIMPLE-ERROR "~S is not related to ~S" {1007070463}>
[1] CL-USER>
; Evaluation aborted on #<SIMPLE-ERROR "~S is not related to ~S" {1007070463}>
CL-USER> (mito:select-dao 'user-book (mito:includes 'user))
(#<USER-BOOK {100795B973}> #<USER-BOOK {100795BDA3}>)
#<SXQL-STATEMENT: SELECT * FROM user_book>
CL-USER> |
Beta Was this translation helpful? Give feedback.
4 replies
-
|
Also, we can override the (defmethod tags (obj)
;; null case
nil)
(defmethod tags ((obj book))
(let ((book-tags
(mito:select-dao 'book-tags
(sxql:where (:= :book-id
(mito:object-id obj))))))
(when book-tags
(mapcar #'tag book-tags))))(tags (mito:find-dao 'book))
;; => (#<TAG tag1> #<TAG tag2>)
(tags (mito:find-dao 'book :id 2))
;; NILSo how could we tell Mito to do that automatically? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
How do you create and use many-to-many relationships?
I find them a bit finicky so any experience sharing will be appreciated.
At first, I had issues defining the intermediate table that references two tables, because the second one was not yet defined.
This doesn't always happen…
Then, getting the data of our many-to-many relationships is not automatic. We need to query the intermediate table with a helper function. Something more automatic or built-in would be nice.
For my example below, note that I always defined my own accessors, because I think Mito is supposed to create default accessors for the relations but they don't always work.
Beta Was this translation helpful? Give feedback.
All reactions