ORMLite on Android throws an exception when creating a table with a column of the type enum and a default value
One of my business objects got a member of the type CommunicationMedium which is an enumeration.
public enum CommunicationMedium {
EMAIL,
PHONE,
MOBILE,
FACSIMILE,
HOMEPAGE
}
The bus开发者_高级运维iness object called CommunicationData is annotated like this.
@DatabaseTable(tableName="store_communication_data")
public class CommunicationData{
public static final String STORE_ID ="store_id";
public static final String TABLE_NAME = "store_communication_data";
public static final String MEDIUM_COLUMN_NAME = "medium";
public static final String VALUE_COLUMN_NAME = "value";
@DatabaseFieldSimple(canBeNull=false, columnName=STORE_ID)
@DatabaseFieldForeign(foreign=true)
private Store store;
@DatabaseFieldSimple(canBeNull=false, columnName=MEDIUM_COLUMN_NAME, defaultValue="CommunicationMedium.EMAIL")
@DatabaseFieldOther(dataType=DataType.ENUM_STRING)
private CommunicationMedium communicationMedium;
@DatabaseFieldSimple(canBeNull=false, columnName=VALUE_COLUMN_NAME)
private String value;
...
}
When accessing the database helper the first time following exception is thrown when the table for the CommunicationData
class should get created. There seem to be a problem with the default value for the medium
column.
java.sql.SQLException: SQL statement failed: CREATE TABLE `store_communication_data` (`medium` __ormlite__ no default value string was specified , `store_id` INTEGER NOT NULL , `value` VARCHAR NOT NULL )
at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
at com.j256.ormlite.table.TableUtils.doStatements(TableUtils.java:458)
at com.j256.ormlite.table.TableUtils.doCreateTable(TableUtils.java:436)
at com.j256.ormlite.table.TableUtils.createTable(TableUtils.java:223)
at com.j256.ormlite.table.TableUtils.createTable(TableUtils.java:53)
...
Caused by: java.sql.SQLException: Problems executing Android statement: CREATE TABLE `store_communication_data` (`medium` __ormlite__ no default value string was specified , `store_id` INTEGER NOT NULL , `value` VARCHAR NOT NULL )
at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
at com.j256.ormlite.android.AndroidCompiledStatement.runUpdate(AndroidCompiledStatement.java:71)
at com.j256.ormlite.table.TableUtils.doStatements(TableUtils.java:452)
...
Caused by: android.database.sqlite.SQLiteException: near "string": syntax error: , while compiling: CREATE TABLE `store_communication_data` (`medium` __ormlite__ no default value string was specified , `store_id` INTEGER NOT NULL , `value` VARCHAR NOT NULL )
at android.database.sqlite.SQLiteProgram.native_compile(Native Method)
at android.database.sqlite.SQLiteProgram.compile(SQLiteProgram.java:110)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1026)
...
Crap. This is a bug that was introduced with the columnDefinition
feature in version 4.26 and was fixed in 4.27. If you use the @DatabaseFieldOther
annotation without specifying the columnDefinition
, you will trigger this unfortunate bug. You can work around it for now @Flo by adding a columnDefinition
value:
@DatabaseFieldSimple(canBeNull = false, columnName = MEDIUM_COLUMN_NAME, defaultValue = "EMAIL")
@DatabaseFieldOther(dataType = DataType.ENUM_STRING, columnDefinition = "VARCHAR(100) DEFAULT 'EMAIL' NOT NULL")
private CommunicationMedium communicationMedium;
The bug was added to our bug tracking system and I've fixed it in version 4.27. Sorry.
精彩评论