1

私は play フレームワーク 2.1 を使用しており、evolution を使用して SQL スクリプトを自動的に生成しています。私が使用しているデータベースは sqlite3 です。

進化は sqlite3 の正しいスクリプトを生成できないようです。私が得た間違ったスクリプトは次のとおりです。

create table algorithm (

id                        bigint AUTOINCREMENT primary key,

name                      varchar(255),

description               varchar(255))

;

私はsqlite3が初めてです。オンラインで検索すると、次のことがわかりました。

  1. autoincrementでのみ動作しますInteger
  2. autoincrementの後に配置する必要がありますprimary key

したがって、明らかに自動生成されたスクリプトは正しくありません。私の質問は次のとおりです。

  1. sqlite3 で引き続き進化を使用できるように、上記の問題を修正する方法はありますか?
  2. 一般的に、進化を使用することをお勧めしますか、またはそれを無効にして、SQL スクリプトを手動で記述しますか?
4

1 に答える 1

1

昨日、同じ問題に遭遇しました。問題は、ebean フレームワークの sqlite ddl 構成内にあります。

しかし、com.avaje.ebean.config.dbplatform.SQLitePlatform クラスの独自の実装を作成することで、この問題を回避することができました。autoincrement キーワードを空の文字列に設定し、bigint 型を整数に再定義する簡単なハックです。

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 ファイルにパッケージ化します。Play アプリケーションのディレクトリ内に配置するlibと、クラスローダーは元の実装の前にクラスをロードし、sqlite はこのカスタム実装によって生成された ddl を受け入れる必要があります。

于 2013-04-30T13:26:58.987 に答える