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

Timo Myyrä timo.myyra at bittivirhe.fi
Tue Jun 9 20:01:09 UTC 2020


Hi,

So the best bet would be to just use the create-table to create the database
structure and then define dao classes separately.
I was trying to avoid that as I'd guess it will be easy to have definitions given on
create-table and dao class to drift apart. I was trying to avoid that by using
just dao so database stuff would be given on just one place. 

But now that we're talking daos, how should I handle references to other tables
with dao class? If we take the User dao class for example, I add the users table
and define the user dao. I have then separate passwords table that stores user
passwords. It doesn't feel right to make passwords an dao class. The passwords
are tied very tighly to user so it would seeem logical to query the passwords with user
dao. What would be 'idiomatic way' to do this with postmodern? Just use the user dao
class and add an method to it, which makes normal sql query for users passwords?
Just define password dao and query it with user id?

A bit basic questions. The documents and examples show small cases so its hard
to see the big picture from them.

timo

Sabra Crolleton <sabra.crolleton at gmail.com> writes:

> 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
>>
>>



More information about the postmodern-devel mailing list