dieselpoint / norm

Access a database in one line of code.
Other
205 stars 36 forks source link

DEFAULT values #47

Open ZZerog opened 4 years ago

ZZerog commented 4 years ago

How catch created timestamp NOT NULL DEFAULT NOW() column with single pojo object?

I tried

    @GeneratedValue
    public Timestamp created;

with result: com.dieselpoint.norm.DbException: org.postgresql.util.PSQLException: Bad value for type long : 2020-03-30 22:46:54.724897

Thx

ccleve commented 4 years ago

Yes, by coincidence I ran into the same problem yesterday. My code assumed that generated values would only be int or long, which is obviously wrong. I need to redesign it.

There is a workaround: execute the insert or update as a query:

String sql = "insert into foo (bar) values ('bah') returning created";

Timestamp created = db.sql(sql).first(Timestamp.class);

The Postgres "returning" keyword returns a result set. You can do "returning *" to get all columns.