How to create dao class with identity column as primary key?

Sabra Crolleton sabra.crolleton at gmail.com
Tue Jun 9 17:34:21 UTC 2020


Hello Timo,

A couple of notes here. First "user" is a restricted word for postgresql,
so I
suggest naming the table "users" instead.

Second, your s-sql sample create table misplaced a paren. You need another
paren after
the username column and before the primary key

I agree that daos do not yet have identity columns, but that really only
prevents you from creating a table using the dao.

So consider the following where we create a table using s-sql, insert some
items,
demonstrate that we can retrieve an item using a dao, then demonstrate we
can
create a dao item, insert it in the table and then retrieve it. Postgresql
handles all
the identity stuff. Also note that I used "users" as the table, but I can
create a dao class
named "user".

(query (:create-table (:if-not-exists 'users)
                      ((id :type integer :identity-always t)
                       (username :type text))
                      (:primary-key id)))

(query (:insert-rows-into 'users :columns 'username :values '(("Jason")
("Tim") ("Karolyn"))))

(defclass user ()
  ((id :col-type integer :accessor id)
   (username :col-type text :initarg :username :accessor username))
  (:metaclass dao-class)
  (:table-name users)
  (:keys id))

(username (get-dao 'user 1))
"Jason"

(let ((item (make-instance 'user :username "Zenya")))
  (insert-dao item))

(username (get-dao 'user 4))

"Zenya"

Does this help?

Sabra Crolleton


On Tue, Jun 9, 2020 at 12:41 PM Timo Myyrä <timo.myyra at bittivirhe.fi> wrote:

> Hi,
>
> I'm learning to use postmodern as part of my hobby project but I've hit a
> small bump in the process.
> I can have the identity column defined for table without dao with
> something like:
> (s-sql:sql (:create-table (:if-not-exists 'user)
>                   ((id :type int :identity-always t)
>                    (username :type text)
>                   (:primary-key id)))
>
> But I intent to use dao classes so it would be nice to have identity
> column specified as part to defclass slot options.
> Is there some way to create dao class with identity column?
>
> Br,
> Timo M
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/postmodern-devel/attachments/20200609/77c74396/attachment.htm>


More information about the postmodern-devel mailing list