Note that there are some explanatory texts on larger screens.

plurals
  1. POGrails how to map enum to existing enum on database
    text
    copied!<p>I have a given <strong>postgres</strong> db using a enum type with following values "M", "F". The value can be null. When i try to save a domainobject, i receive the errors below. </p> <p>postgres enum:</p> <pre><code>CREATE TYPE genderlist AS ENUM ('M','F') </code></pre> <p>my enum:</p> <pre><code>public enum GenderCode { M,F private final String value private static list public String value() { return name() } public static GenderCode fromValue(String v) { return valueOf(v) } } </code></pre> <p>domain class:</p> <pre><code>class UserLog { String gender = null //default null .. } </code></pre> <p>BootStrap.groovy:</p> <pre><code>// reading - works fine now def rows = UserLog.list() // insert - does not work UserLog u = new UserLog(gender:null) u.save() </code></pre> <p>Error:</p> <pre><code>| Batch-Entry 0 insert into user_log (active, birthyear, country, gender, user_id, id) values ('1', NULL, NULL, NULL, NULL, 98304) was cancelled. | ERROR: column "gender" is of type genderlist but expression is of type character varying Note: You will need to rewrite or cast the expression. </code></pre> <hr> <p><strong>EDIT</strong><br> i updated the whole question to match the current state of my project. since i updated the enum i do not longer have problems on reading data. the PGObject gets casted into the String representation properly. the casting is still an issue when trying to insert data.</p> <p>i have a dirty little workaround which works:</p> <pre><code> def sql = new Sql(dataSource_log) def map = [gender:'M'] sql.executeInsert "insert into user_log (gender) values ('${map.gender}')" </code></pre> <p>my question still stays the same: how do i cast the String properly <strong>on insert?</strong> also 'null' value is not accepted yet. Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload