fadedbee
fadedbee

Reputation: 44735

Following "getting started" diesel tutorial, but with sqlite, causes

I'm trying to follow: https://diesel.rs/guides/getting-started but I'm using:

echo DATABASE_URL=/tmp/diesel_demo.sqlite > .env

instead of a Postgres database.

I've changed all occurrences of Pg to Sqlite, and SERIAL to INT, but get the following error:

error[E0277]: the trait bound `i32: FromSql<diesel::sql_types::Nullable<diesel::sql_types::Integer>, Sqlite>` is not satisfied
  --> src/bin/show_posts.rs:14:10
   |
14 |         .load::<Post>(&connection)
   |          ^^^^ the trait `FromSql<diesel::sql_types::Nullable<diesel::sql_types::Integer>, Sqlite>` is not implemented for `i32`    
How to get a result set where field value == row number?

show_posts.rs:

extern crate diesel_demo;
extern crate diesel;

use self::diesel_demo::*;
use self::models::*;
use self::diesel::prelude::*;

fn main() {
    use diesel_demo::schema::posts::dsl::*;

    let connection = establish_connection();
    let results = posts.filter(published.eq(true))
        .limit(5)
        .load::<Post>(&connection)
        .expect("Error loading posts");

    println!("Displaying {} posts", results.len());
    for post in results {
        println!("{}", post.title);
        println!("----------\n");
        println!("{}", post.body);
    }
}

up.sql:

CREATE TABLE posts (
  id INTEGER PRIMARY KEY,
  title VARCHAR NOT NULL,
  body TEXT NOT NULL,
  published BOOLEAN NOT NULL DEFAULT 'f'
)

models.rs (autogenerated):

#[derive(Queryable)]
pub struct Post {
    pub id: i32,
    pub title: String,
    pub body: String,
    pub published: bool,
}

I don't understand why Diesel expects id to be Nullable.

Upvotes: 7

Views: 4873

Answers (1)

fadedbee
fadedbee

Reputation: 44735

Adding NOT NULL to the id field in up.sql fixed it.

Upvotes: 10

Related Questions