Fix behaviour with auto-inc primary keys.
authorRupert Swarbrick <rswarbrick@gmail.com>
Sat, 25 Sep 2010 15:42:03 +0000 (16:42 +0100)
committerKevin Rosenberg <kevin@rosenberg.net>
Sun, 7 Nov 2010 16:35:45 +0000 (09:35 -0700)
This patch sets the slot value of the field containing the primary key
to the one that the database gave it. Before, it wasn't saved so then
changes to other slots didn't update the database (since the query
ended up looking something like "UPDATE blah WHEN id = 'NULL' ...",
which obviously doesn't work).

sql/oodml.lisp

index 710e5e8e090c45548e4a41d6948ccd35c7c7536c..7bf7d5bf77db5adcb9bed1ca976ca7082a2afebb 100644 (file)
                                          (ordered-class-direct-slots view-class)
                                          (ordered-class-slots view-class))))
                (record-values (mapcar #'slot-value-list slots)))
+
           (cond ((and (not (normalizedp view-class))
                       (not record-values))
                  (error "No settable slots."))
                  (insert-records :into (sql-expression :table view-class-table)
                                  :av-pairs record-values
                                  :database database)
+
                  (when pk-slot
                    (if (or (and (listp (view-class-slot-db-constraints pk-slot))
                                 (member :auto-increment (view-class-slot-db-constraints pk-slot)))
                            (eql (view-class-slot-db-constraints pk-slot) :auto-increment))
+                       (unless pk
+                         (let ((db-pk (car (query "SELECT LAST_INSERT_ID();"
+                                                  :flatp t :field-names nil
+                                                  :database database))))
+                           (setf pk db-pk
+                                 (slot-value
+                                  obj (slot-definition-name pk-slot)) db-pk)))
+
                        (setf pk (or pk
-                                    (car (query "SELECT LAST_INSERT_ID();"
-                                                :flatp t :field-names nil
-                                                :database database))))
-                       (setf pk (or pk
-                                    (slot-value obj (slot-definition-name pk-slot))))))
+                                    (slot-value
+                                     obj (slot-definition-name pk-slot))))))
                  (when (eql this-class nil)
                    (setf (slot-value obj 'view-database) database)))))))
     pk))