Skip to main content
Version: 0.12.x

Using SQLite

If you want to test application logic that does not require database-specific features, SQLite will be a good choice for you.

Check out a simple example here.

Integration Test

It is recommended to execute more complex test cases in integration tests. The following code snippet illustrates the steps of connecting to a database, setting up schema and performing tests.

async fn main() -> Result<(), DbErr> {
// Connecting SQLite
let db = Database::connect("sqlite::memory:").await?;

// Setup database schema

// Performing tests


Setup database schema

To create tables in SQLite database for testing, instead of writing TableCreateStatement manually, you can derive it from Entity using Schema::create_table_from_entity.

async fn setup_schema(db: &DbConn) {

// Setup Schema helper
let schema = Schema::new(DbBackend::Sqlite);

// Derive from Entity
let stmt: TableCreateStatement = schema.create_table_from_entity(MyEntity);

// Or setup manually

// Execute create table statement
let result = db

Performing tests

Execute test cases and assert against the results.

async fn testcase(db: &DbConn) -> Result<(), DbErr> {

let baker_bob = baker::ActiveModel {
name: Set("Baker Bob".to_owned()),
contact_details: Set(serde_json::json!({
"mobile": "+61424000000",
"home": "0395555555",
"address": "12 Test St, Testville, Vic, Australia"
bakery_id: Set(2),

let baker_insert_res = Baker::insert(baker_bob)
.expect("could not insert baker");

assert_eq!(baker_insert_res.last_insert_id, 1);