sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

Column output override type truncates itself after ~64 characters

Open KennanHunter opened this issue 3 months ago • 1 comments

I have found these related issues/pull requests

No related issues found.

Description

If the field name + type name is too long, sqlx will truncate the type name. This is an error generated by cargo build

error[E0412]: cannot find type `ThisIsAExcessiv` in this scope
   --> 
    |
  1 | ...   sqlx::query!(r#"SELECT count(*) as "excessively_long_field_name_used_for_reference: ThisIsAExcessivelyLongNameToGetErrorWithTypeNaming" FROM users"#) 
    |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
not found in this scope
    |
    = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query` (in Nightly builds, run with -Z macro-backtrace for more info)

Reproduction steps

#[tokio::test]
async fn big_type_name() {
    let pool = sqlx::PgPool::connect()
        .await
        .expect("Can connect to postgres");

    #[derive(sqlx::Type, Debug)]
    #[sqlx(transparent)]
    struct ThisIsAExcessivelyLongNameToGetErrorWithTypeNaming(i64);

    sqlx::query!(r#"SELECT count(*) as "excessively_long_field_name_used_for_reference: ThisIsAExcessivelyLongNameToGetErrorWithTypeNaming" FROM users"#)
        .fetch_one(&pool)
        .await
        .expect("Can query");
}

SQLx version

0.8.6

Enabled SQLx features

features = [ "postgres", "runtime-tokio", "tls-native-tls" ]

Database server and version

Postgres 16

Operating system

Windows

Rust version

rustc 1.90.0 (1159e78c4 2025-09-14)

KennanHunter avatar Sep 23 '25 02:09 KennanHunter

This unfortunately was to be expected, because Postgres only supports identifiers up to 63 bytes in length: https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

The system uses no more than NAMEDATALEN-1 bytes of an identifier; longer names can be written in commands, but they will be truncated. By default, NAMEDATALEN is 64 so the maximum identifier length is 63 bytes. If this limit is problematic, it can be raised by changing the NAMEDATALEN constant in src/include/pg_config_manual.h.

This is perhaps another reason to actually parse the SQL, as we can extract the identifier before it's truncated. However, we would still need to connect to the database at compile time for proper typechecking.

abonander avatar Sep 23 '25 14:09 abonander