rustautomationmodelrust-diesel

Diesel models generator


I am using this command to generate models with diesel cli: diesel_ext --model > src/models.rs

After i run that command i got:

// Generated by diesel_ext

#![allow(unused)]  
#![allow(clippy::all)]


use uuid::Uuid;
use chrono::DateTime;
use chrono::offset::Utc;
#[derive(Queryable, Debug)]

pub struct UserRole {
  pub id: Uuid,
  pub user_id: Uuid,
  pub role_name: /* TODO: unknown type Rolename */,
  pub created_at: Option<DateTime<Utc>>,
  pub updated_at: Option<DateTime<Utc>>,
}

#[derive(Queryable, Debug)]
pub struct UserToken {
  pub id: Uuid,
  pub user_id: Uuid,
  pub token: /* TODO: unknown type Tokentype */,
  pub value: String,
  pub created_at: Option<DateTime<Utc>>,
  pub updated_at: Option<DateTime<Utc>>,
}

#[derive(Queryable, Debug)]
  pub struct User {
  pub id: Uuid,
  pub email: String,
  pub password_hash: String,
  pub mobile: String,
  pub created_at: Option<DateTime<Utc>>,
  pub updated_at: Option<DateTime<Utc>>,
}

I got problem with role_name and token (tokentype) fileds in generated structs. I got message in terminal that:

Rolename and Tokentype is not recognized, Please feel free to expand the HashMap,This could provide good hints: https://kotiri.com/2018/01/31/postgresql-diesel-rust-types.html

Generated Schema:

// @generated automatically by Diesel CLI.

pub mod sql_types {
    #[derive(diesel::sql_types::SqlType)]
    #[diesel(postgres_type(name = "rolename"))]
    pub struct Rolename;

    #[derive(diesel::sql_types::SqlType)]
    #[diesel(postgres_type(name = "tokentype"))]
    pub struct Tokentype;
}

diesel::table! {
    use diesel::sql_types::*;
    use super::sql_types::Rolename;

    user_roles (id) {
        id -> Uuid,
        user_id -> Uuid,
        role_name -> Rolename,
        created_at -> Nullable<Timestamptz>,
        updated_at -> Nullable<Timestamptz>,
    }
}

diesel::table! {
    use diesel::sql_types::*;
    use super::sql_types::Tokentype;

    user_tokens (id) {
        id -> Uuid,
        user_id -> Uuid,
        token -> Tokentype,
        value -> Text,
        created_at -> Nullable<Timestamptz>,
        updated_at -> Nullable<Timestamptz>,
    }
}

diesel::table! {
    users (id) {
        id -> Uuid,
        #[max_length = 255]
        email -> Varchar,
        #[max_length = 255]
        password_hash -> Varchar,
        #[max_length = 255]
        mobile -> Varchar,
        created_at -> Nullable<Timestamptz>,
        updated_at -> Nullable<Timestamptz>,
    }
}

diesel::joinable!(user_roles -> users (user_id));
diesel::joinable!(user_tokens -> users (user_id));

diesel::allow_tables_to_appear_in_same_query!(
    user_roles,
    user_tokens,
    users,
);

It seems like it can't generate the types for structs as needed.

SQL Tables (Postgres):

create table users(
    id uuid not null primary key default gen_random_uuid(),
    email varchar(255) not null unique,
    password_hash varchar(255) not null,
    mobile varchar(255) not null unique,

    created_at timestamp with time zone default now(),
    updated_at timestamp with time zone
);

create type RoleName as enum ('Admin','Customer','Guest','Alpha','Beta', 'Gamma');

create table user_roles(
    id uuid not null primary key default gen_random_uuid(),
    user_id UUID not null references users(id) on delete cascade,
    role_name RoleName not null default 'Guest',

    created_at timestamp with time zone default now(),
    updated_at timestamp with time zone
);

create type TokenType as enum ('Access', 'Refresh', 'EmailVerification', 'PasswordReset', 'Session', 'PhoneVerification');

create table user_tokens(
    id uuid not null primary key default gen_random_uuid(),
    user_id UUID not null references users(id) on delete cascade,
    token TokenType not null,
    value text not null,

    created_at timestamp with time zone default now(),
    updated_at timestamp with time zone
);

Is there anyway i can set up the generation of types into structs in that case?


Solution

  • Reading between the lines of this issue - Generate Custom Types for certain Columns from tables - this is not supported; the tool will not generate Rust enums from the Postgres enums.

    The workaround is to define the Rust enum types yourself and tell diesel_ext to use them via a --map/-m flag (and maybe an --import/-i flag):

    diesel_ext --model -m "TokenType TokenType" -m "RoleType RoleType" > src/models.rs