Support for default values from the DB column definition

143 views
Skip to first unread message

rwfischer

unread,
Apr 26, 2017, 6:08:53 PM4/26/17
to mybatis-user
It appears that the MyBatis generator 1.3.5 does not support default values defined with the DB column.

I am using Java and PostgreSQL, and I have many columns that are defined as "not null default xyz". When I insert a new row using MyBatis and do not specify a value for such a column, I get the following error.
ERROR: null value in column "creation_time" violates not-null constraint

MyBatis should be aware that the column has a default value, and thus should accept the null value.

Is this a bug, or a missing feature? If the latter, could it be added. I should think that the scenario is common enough.

Jeff Butler

unread,
Apr 26, 2017, 8:23:57 PM4/26/17
to mybatis-user
If a column has a default value, then it should not be included in the insert statement if you want to use the default value.  If you are using generated code, you can use the insertSelective method - it will ignore columns with null values.

Jeff Butler


--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

rwfischer

unread,
Apr 27, 2017, 5:43:11 PM4/27/17
to mybatis-user
Thanks, Jeff. That makes sense.

We are actually migrating from iBatis, and we had customized Abator to handle the defaults (it was a fairly simple change. At the time, iBatis did not have a insertSelective.

Using insertSelective() will require a lot of application code changes, and we were looking at ways to avoid that.

Roger
Reply all
Reply to author
Forward
0 new messages