What are models.rs and schema.rs in rocket web framework?

hello, I have following some tutorial on internet about rocket web framework, but I still I don't know actually what is schema and models, can anyone explain ?

my schema.rs

// @generated automatically by Diesel CLI.

diesel::table! {
    crates (id) {
        id -> Int4,
        rustacean_id -> Int4,
        #[max_length = 64]
        code -> Varchar,
        #[max_length = 128]
        name -> Varchar,
        #[max_length = 64]
        version -> Varchar,
        description -> Nullable<Text>,
        created_at -> Timestamp,
    }
}

diesel::table! {
    rustaceans (id) {
        id -> Int4,
        name -> Varchar,
        email -> Varchar,
        created_at -> Timestamp,
    }
}

diesel::joinable!(crates -> rustaceans (rustacean_id));

diesel::allow_tables_to_appear_in_same_query!(
    crates,
    rustaceans,
);

my models.rs

use diesel::Insertable;
use chrono::NaiveDateTime;
use crate::schema::{rustaceans, crates};


pub struct Rustacean {
    pub id: i32,
    pub name: String,
    pub email: String,
    pub created_at: NaiveDateTime
}

#[derive(Insertable)]
#[diesel(table_name=rustaceans)]
pub struct NewRustacean {
    pub name: String,
    pub email: String
}

pub struct Crate {
    pub id: i32,
    pub rustacean_id: i32,
    pub code: String,
    pub name: String,
    pub version: String,
    pub description: Option<String>, //why using Option ? this indicate the field is not null(that mean can be empty)
    pub created_at: NaiveDateTime,
}

#[derive(Insertable)]
#[diesel(table_name=crates)]
pub struct NewCrate {
    pub rustacean_id: i32,
    pub code: String,
    pub name: String,
    pub version: String,
    pub description: Option<String>
}

This is not Rocket per se, but comes from the diesel crate. Diesel provides an ORM (object-relational mapping) for your SQL database, mapping your database tables to structs you can use in your web application with ease. Diesel has a lot of documentation, I suggest you start with the Getting Started guide if you want to learn more about it.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.