在Play框架中ebean为sqlite数据库生成的错误SQL脚本 [英] Wrong sql script generated for sqlite database by ebean in the Play framework
问题描述
我正在使用play框架2.1,并使用Evolution为我自动生成sql脚本.我使用的数据库是sqlite3.
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.
似乎进化无法为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))
;
我是sqlite3的新手.通过在线搜索,我意识到:
I am new to sqlite3. By searching online I realized that:
-
autoincrement
仅可与Integer
一起使用
-
autoincrement
应该放在primary key
之后
autoincrement
can only works withInteger
autoincrement
should be placed afterprimary key
因此,显然自动生成的脚本是错误的.我的问题是:
So obviously the automatically generated script is incorrect. My questions are:
- 有什么办法可以解决上述问题,以便我仍然可以在sqlite3中使用Evolution?
- 通常,您是否建议使用Evolution或禁用它并手动编写sql脚本?
推荐答案
昨天我遇到了同一问题.该问题位于ebean框架的sqlite ddl配置内部.
I ran into the same issue yesterday. The problem is located inside the sqlite ddl configuration of the ebean framework.
但是,通过创建自己的com.avaje.ebean.config.dbplatform.SQLitePlatform类的实现,我能够解决该问题.这是一个快速的技巧,它将autoincrement关键字设置为一个空字符串,并将bigint类型重新定义为整数:
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;
}
}
编译该类并将其打包到jar文件中.
当放置在播放应用程序的lib
目录中时,类加载器将在原始实现之前加载类,而sqlite应接受此自定义实现生成的ddl.
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.
这篇关于在Play框架中ebean为sqlite数据库生成的错误SQL脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!