diff --git a/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl.java b/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl.java index 3843507cf..7328bfd54 100644 --- a/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl.java +++ b/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl.java @@ -176,7 +176,7 @@ public class PlatformDdl { buffer.append(" "); buffer.append(lowerColumnName(column.getName()), 29); buffer.append(platformType); - if (!typeContainsDefault(platformType)) { + if (!Boolean.TRUE.equals(column.isPrimaryKey()) && !typeContainsDefault(platformType)) { String defaultValue = convertDefaultValue(column.getDefaultValue()); if (defaultValue != null) { buffer.append(" default ").append(defaultValue); diff --git a/src/main/java/io/ebean/dbmigration/model/build/ModelBuildPropertyVisitor.java b/src/main/java/io/ebean/dbmigration/model/build/ModelBuildPropertyVisitor.java index 2b3bdbeac..b867eff57 100644 --- a/src/main/java/io/ebean/dbmigration/model/build/ModelBuildPropertyVisitor.java +++ b/src/main/java/io/ebean/dbmigration/model/build/ModelBuildPropertyVisitor.java @@ -221,7 +221,6 @@ public class ModelBuildPropertyVisitor extends BaseTablePropertyVisitor { // using non-strict mode to render the DB type such that we have a // "logical" type like jsonb(200) that can map to JSONB or VARCHAR(200) MColumn col = new MColumn(p.getDbColumn(), ctx.getColumnDefn(p, false)); - col.setDefaultValue(p.getDbColumnDefault()); col.setComment(p.getDbComment()); col.setDraftOnly(p.isDraftOnly()); col.setHistoryExclude(p.isExcludedFromHistory()); @@ -231,8 +230,11 @@ public class ModelBuildPropertyVisitor extends BaseTablePropertyVisitor { if (p.getBeanDescriptor().isUseIdGenerator()) { col.setIdentity(true); } - } else if (!p.isNullable() || p.isDDLNotNull()) { - col.setNotnull(true); + } else { + col.setDefaultValue(p.getDbColumnDefault()); + if (!p.isNullable() || p.isDDLNotNull()) { + col.setNotnull(true); + } } if (p.isUnique() && !p.isId()) { diff --git a/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanProperty.java b/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanProperty.java index 578ab0e74..2a08d742c 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanProperty.java +++ b/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanProperty.java @@ -34,6 +34,8 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.sql.Types; +import java.util.HashSet; +import java.util.Set; /** * Description of a property of a bean. Includes its deployment information such @@ -45,6 +47,14 @@ public class DeployBeanProperty { private static final int UNIDIRECTIONAL_ORDER = 100000; private static final int AUDITCOLUMN_ORDER = -1000000; private static final int VERSIONCOLUMN_ORDER = -1000000; + private static final Set> PRIMITIVE_NUMBER_TYPES = new HashSet<>(); + static { + PRIMITIVE_NUMBER_TYPES.add(float.class); + PRIMITIVE_NUMBER_TYPES.add(double.class); + PRIMITIVE_NUMBER_TYPES.add(long.class); + PRIMITIVE_NUMBER_TYPES.add(int.class); + PRIMITIVE_NUMBER_TYPES.add(short.class); + } /** * Flag to mark this at part of the unique id. @@ -940,6 +950,9 @@ public class DeployBeanProperty { if (boolean.class.equals(propertyType) && !softDelete) { this.nullable = false; this.dbColumnDefault = DbDefaultValue.FALSE; + + } else if (!id && !versionColumn && PRIMITIVE_NUMBER_TYPES.contains(propertyType)) { + this.nullable = false; } }