Code Monkey home page Code Monkey logo

Comments (3)

C-Entropy avatar C-Entropy commented on August 17, 2024

It turns out that (slot-value 'foo-1 'parent-column-map), (see here) doesn't have file-dao. Is this a design on purpose?

from mito.

fukamachi avatar fukamachi commented on August 17, 2024

Seems to be a bug.

from mito.

C-Entropy avatar C-Entropy commented on August 17, 2024

Here is my simple work around @fukamachi

(defun add-referencing-slots (initargs)
  (let ((parent-column-map NIL))
    (loop for column in (getf initargs :direct-superclasses)
	  when (and (not (eq 'standard-class (type-of column)))
		    (slot-value column 'parent-column-map))
	    do (alexandria:unionf parent-column-map
				  (alexandria:hash-table-alist (slot-value column 'parent-column-map))))
    (setf parent-column-map (alexandria:alist-hash-table parent-column-map :test 'eq))
    (setf (getf initargs :direct-slots)
          (loop for column in (getf initargs :direct-slots)
                for (col-type not-null) = (multiple-value-list (parse-col-type (getf column :col-type)))

                if (typep col-type '(and symbol (not null) (not keyword)))
                  append
                  (let* ((name (getf column :name))
                         ;; FIXME: find-class raises an error if the class is this same class or not defined yet.
                         (rel-class (find-class col-type))
                         (pk-names (table-primary-key rel-class)))
                    (unless pk-names
                      (error "Foreign class ~S has no primary keys."
                             (class-name rel-class)))
                    (rplacd (cdr column)
                            `(:ghost t ,@(cddr column)))

                    (cons column
                          (mapcar (lambda (pk-name)
                                    (let ((rel-column-name (rel-column-name name pk-name)))
                                      (setf (gethash rel-column-name parent-column-map) name)
                                      `(:name ,rel-column-name
                                        :initargs (,(intern (symbol-name rel-column-name) :keyword))
                                        :col-type ,(if not-null
                                                       col-type
                                                       `(or ,col-type :null))
                                        :primary-key ,(getf column :primary-key)
                                        :references (,col-type ,pk-name))))
                                  pk-names)))
                collect column))
    (values initargs parent-column-map)))

(defmethod initialize-instance :around ((class table-class) &rest initargs)
  (multiple-value-bind (initargs parent-column-map)
      (add-referencing-slots initargs)
    (let ((class (apply #'call-next-method class initargs)))
      (setf (slot-value class 'parent-column-map)
	    (alexandria:alist-hash-table
	     (union (alexandria:hash-table-alist (slot-value class 'parent-column-map))
		    (alexandria:hash-table-alist parent-column-map))))
      (expand-relational-keys class 'primary-key)
      (expand-relational-keys class 'unique-keys)
      (expand-relational-keys class 'keys)
      class)))

Should work with the simplest case.

from mito.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.