问题
I am using the play framework 2.1 and using the evolution to automatically generate sql scripts for me. The database I use is sqlite3.
It seems the evolution can't generate correct script for sqlite3. The incorrect script I got is:
create table algorithm (
id bigint AUTOINCREMENT primary key,
name varchar(255),
description varchar(255))
;
I am new to sqlite3. By searching online I realized that:
autoincrement
can only works withInteger
autoincrement
should be placed afterprimary key
So obviously the automatically generated script is incorrect. My questions are:
- Is there any way to fix the above issue so that I can still use evolution with sqlite3?
- In general do you suggest using evolution or disable it and write sql scripts manually?
回答1:
I ran into the same issue yesterday. The problem is located inside the sqlite ddl configuration of the ebean framework.
However I was able to work around the problem, by creating my own implementation of the com.avaje.ebean.config.dbplatform.SQLitePlatform class. It's a quick hack which sets the autoincrement keyword to an empty string and redefines the bigint type to integer:
package com.avaje.ebean.config.dbplatform;
import java.sql.Types;
import javax.sql.DataSource;
import com.avaje.ebean.BackgroundExecutor;
public class SQLitePlatform extends DatabasePlatform {
static {
System.err.println("\n\n!!! Custom SQLitePlatform class for ebean ORM loaded !!!!\n\n");
}
public SQLitePlatform() {
super();
this.name = "sqlite";
this.dbIdentity.setIdType(IdType.IDENTITY);
this.dbIdentity.setSupportsGetGeneratedKeys(false);
this.dbIdentity
.setSelectLastInsertedIdTemplate("select last_insert_rowid()");
this.openQuote = "\"";
this.closeQuote = "\"";
this.booleanDbType = Types.INTEGER;
dbTypeMap.put(Types.BIT, new DbType("int default 0"));
dbTypeMap.put(Types.BOOLEAN, new DbType("int default 0"));
dbTypeMap.put(Types.BIGINT, new DbType("integer"));
dbDdlSyntax.setInlinePrimaryKeyConstraint(true);
dbDdlSyntax.setIdentity("");
dbDdlSyntax.setDisableReferentialIntegrity("PRAGMA foreign_keys = OFF");
dbDdlSyntax.setEnableReferentialIntegrity("PRAGMA foreign_keys = ON");
}
/**
* Return null in case there is a sequence annotation.
*/
@Override
public IdGenerator createSequenceIdGenerator(BackgroundExecutor be,
DataSource ds, String seqName, int batchSize) {
return null;
}
}
Compile and package the class into a jar file.
When placed inside the lib
directory of your play application, the classloader will load the class before the original implementation, and sqlite should accept the ddl generated by this custom implementation.
来源:https://stackoverflow.com/questions/15733941/wrong-sql-script-generated-for-sqlite-database-by-ebean-in-the-play-framework