Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullable<Varchar> generated instead of Varchar #4319

Closed
3 tasks done
SamuelMarks opened this issue Oct 21, 2024 · 3 comments
Closed
3 tasks done

Nullable<Varchar> generated instead of Varchar #4319

SamuelMarks opened this issue Oct 21, 2024 · 3 comments

Comments

@SamuelMarks
Copy link

SamuelMarks commented Oct 21, 2024

Setup

Versions

  • Rust: 1.84.0-nightly (662180b34 2024-10-20)
  • Diesel: 2.2.4
  • Database: PostgreSQL 17.0
  • Operating System macOS 15.0.1

Feature Flags

  • diesel: postgres, r2d2

Problem Description

What are you trying to accomplish?

#[derive(Queryable, Selectable, PartialEq, Debug)]
#[diesel(table_name = crate::schema::users)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct User {
    pub username: String,
    pub password_hash: String,
    pub role: String,
    pub created_at: std::time::SystemTime,
}
CREATE TABLE "users"
(
    username      VARCHAR(50) PRIMARY KEY,
    password_hash VARCHAR(50) NOT NULL,
    role          TEXT        NOT NULL DEFAULT 'regular',
    created_at    TIMESTAMP   NOT NULL DEFAULT current_timestamp
);

What is the expected output?

schema.rs of

diesel::table! {
    users (username) {
        #[max_length = 50]
        username -> Varchar,
        #[max_length = 50]
        password_hash -> Varchar,
        role -> Text,
        created_at -> Timestamp,
    }
}

What is the actual output?

schema.rs of

diesel::table! {
    users (username) {
        #[max_length = 50]
        username -> Varchar,
        #[max_length = 50]
        password_hash -> Nullable<Varchar>,
        role -> Nullable<Text>,
        created_at -> Timestamp,
    }
}

Are you seeing any additional errors?

No

Steps to reproduce

https://github.com/offscale/rust-actix-diesel-auth-scaffold/tree/2f01d2b

Checklist

  • This issue can be reproduced on Rust's stable channel. (Your issue will be
    closed if this is not the case)
  • This issue can be reproduced without requiring a third party crate
SamuelMarks added a commit to SamuelMarks/rust-actix-diesel-auth-scaffold that referenced this issue Oct 21, 2024
@weiznich
Copy link
Member

I cannot reproduce this locally. Additionally diesel does "only" query the database to get information whether or not a column is nullable. So if your schema.rs file says that the column is nullable the database considers it to be nullable. You should be able to verify this by using the following query:

SELECT "information_schema"."columns"."column_name", "information_schema"."columns"."udt_name", "information_schema"."columns"."udt_schema", "information_schema"."columns"."is_nullable", "information_schema"."columns"."character_maximum_length", col_description('users'::regclass, "information_schema"."columns"."ordinal_position") FROM "information_schema"."columns" WHERE (("information_schema"."columns"."table_name" = 'users') AND ("information_schema"."columns"."table_schema" = 'public')) ORDER BY "information_schema"."columns"."ordinal_position"

Please provide the output of that query for further debugging, otherwise this issue will likely be closed as cannot reproduce.

@SamuelMarks
Copy link
Author

Try deleting the schema.rs file. Then run that diesel command again and you'll see it get regenerated. It has Nullables; it shouldn't.

And to answer your question:

  column_name  | udt_name  | udt_schema | is_nullable | character_maximum_length | col_description 
---------------+-----------+------------+-------------+--------------------------+-----------------
 username      | varchar   | pg_catalog | NO          |                       50 | 
 password_hash | varchar   | pg_catalog | YES         |                       50 | 
 role          | text      | pg_catalog | YES         |                          | 
 created_at    | timestamp | pg_catalog | NO          |                          | 
(4 rows)

@weiznich
Copy link
Member

Thanks for providing the output of that query. This output clearly shows that the database allows nullable values for these columns.
At this point it's likely that the provided SQL does not match that SQL what you actually used to create the table. The other possibility is a bug in the database system itself.

Both variants are not an issue in diesel, so I will close this issue now.

@weiznich weiznich closed this as not planned Won't fix, can't repro, duplicate, stale Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants