'Read only' Field Support

28 views
Skip to first unread message

Gary Malouf

unread,
Jan 20, 2016, 10:43:54 PM1/20/16
to Slick / ScalaQuery
We want the created_at and updated_at field support that ActiveRecord gives you out of the box.  Since we are using Postgres (and after reading a number of threads on the topic in this group), we decided that the best way to populate each of these fields was via default values and a database trigger (updates).  

One thing that would be ideal is to have 'read-only' columns.  i.e. I'd like to be able to query these fields at will but not have to worry about them being overwritten by our model objects when doing updates.  Having to manually code custom update functions to avoid this seems painful - is there a better approach?

Gary Malouf

unread,
Jan 22, 2016, 8:28:48 AM1/22/16
to Slick / ScalaQuery
Is there a way to enable 'soft-inserts' and/or updates to get the behavior I am describing?

Marius Muja

unread,
Jan 22, 2016, 2:36:14 PM1/22/16
to scala...@googlegroups.com
You can get this behavior by making your update trigger ignore the update value, for example:

CREATE OR REPLACE FUNCTION public.set_updated_timestamp()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
IF OLD.created_at IS NOT NULL THEN
NEW.created_at = OLD.created_at;
END IF;
NEW.updated_at = now();
RETURN NEW;
END;
$function$


--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/9d058571-428c-4e19-a784-04cfb9c01979%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages