fukamachi / mito

An ORM for Common Lisp with migrations, relationships and PostgreSQL support
284 stars 31 forks source link

`:deflate` slot option not being called when defining a relationship #119

Open halcyonseeker opened 1 year ago

halcyonseeker commented 1 year ago

When a column in table A references a row in table B, and when the primary key slot of B has a :deflate function, that function doesn't get called. In the example below, this causes SXQL to choke on a pathname when trying to insert an instance of the user class. Changing it from a pathname to a string and removing the :inflate and :deflate options in file fixes the problem, however it seems to me that those functions should be called transparently.


(require 'mito)

(defclass file ()
  ((path :initarg :path :col-type :text :primary-key t
         :inflate #'pathname :deflate #'namestring)
   (desc :initarg :desc :col-type :text))
  (:metaclass mito:dao-table-class)
  (:unique-keys path)
  (:conc-name file-)
  (:documentation "Record a list of files and their descriptions."))

(defclass user ()
  ((name :initarg :name :col-type :text :primary-key t)
   (file :initarg :file :col-type file))
  (:metaclass mito:dao-table-class)
  (:conc-name user-)
  (:documentation "Record which users own which files."))

(defvar f (make-instance 'file :path #P"/tmp/mito_bug.txt" :desc "some file"))
(defvar u (make-instance 'user :name "Eva Lu Ator" :file f))

(mito:connect-toplevel :sqlite3 :database-name #P"/tmp/mito_bug.db")
(mapcar #'mito:ensure-table-exists '(file user))
;; CREATE TABLE "file" (
;;     "path" TEXT NOT NULL PRIMARY KEY,
;;     "desc" TEXT NOT NULL,
;;     "created_at" TIMESTAMP,
;;     "updated_at" TIMESTAMP,
;;     UNIQUE ("path")
;; ) () [0 rows] | (MITO.DAO:ENSURE-TABLE-EXISTS 'FILE)
;; CREATE TABLE "file" (
;;     "path" TEXT NOT NULL PRIMARY KEY,
;;     "desc" TEXT NOT NULL,
;;     "created_at" TIMESTAMP,
;;     "updated_at" TIMESTAMP,
;;     UNIQUE ("path")
;; ) () [0 rows] | (MITO.DAO:ENSURE-TABLE-EXISTS 'FILE)
;; CREATE TABLE "user" (
;;     "name" TEXT NOT NULL PRIMARY KEY,
;;     "file_path" TEXT NOT NULL,
;;     "created_at" TIMESTAMP,
;;     "updated_at" TIMESTAMP
;; ) () [0 rows] | (MITO.DAO:ENSURE-TABLE-EXISTS 'USER)
;; CREATE TABLE "user" (
;;     "name" TEXT NOT NULL PRIMARY KEY,
;;     "file_path" TEXT NOT NULL,
;;     "created_at" TIMESTAMP,
;;     "updated_at" TIMESTAMP
;; ) () [0 rows] | (MITO.DAO:ENSURE-TABLE-EXISTS 'USER)

(mito:insert-dao f)
(mito:insert-dao u)
;; There is no applicable method for the generic function
;;   #<STANDARD-GENERIC-FUNCTION SXQL.OPERATOR:CONVERT-FOR-SQL (9)>
;; when called with arguments
;;   (#P"/tmp/mito_bug.txt").
;;    [Condition of type SB-PCL::NO-APPLICABLE-METHOD-ERROR]