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

Inconsistent Field Order in PostgreSQL: Risks and Recommendations #3085

Closed
liutaon opened this issue Mar 3, 2024 · 1 comment
Closed

Inconsistent Field Order in PostgreSQL: Risks and Recommendations #3085

liutaon opened this issue Mar 3, 2024 · 1 comment
Labels

Comments

@liutaon
Copy link

liutaon commented Mar 3, 2024

Bug Description

In two different environments (for example, Test and Prod environments), when the order of fields in a table is inconsistent, using select * from table can lead to misplacement of field values or, in more severe cases, a panic.

This is a very serious potential issue. It should be explicitly stated in the documentation that data must be accessed using field names, not by using select * from.

Info

  • SQLx version: 0.7.3
  • SQLx features enabled: "postgres", "runtime-tokio", "json", "macros", "time", "rust_decimal",
  • Database server and version: PostgreSQL 15
  • Operating system: MacOS, Linux
  • rustc --version: rustc 1.76.0 (07dca489a 2024-02-04)

Minimal Reproduction

#[derive(Debug)]
struct User {
    id: i32,
    name: String,
    age: i32,
}

#[tokio::main]
async fn main() {
    dotenvy::dotenv().ok();
    let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
    let pool = sqlx::PgPool::connect(&database_url).await.unwrap();
    let users = sqlx::query_as!(User, "select * from test_user")
        .fetch_all(&pool)
        .await
        .unwrap();
    println!("{:?}", users);
}

Test & Ooutput:

test=> \d test_user

 Column |       Type        | Collation | Nullable | Default 
--------+-------------------+-----------+----------+---------
 id     | integer           |           | not null | 
 name   | character varying |           | not null | 
 age    | integer           |           | not null | 

[User { id: 1, name: "test", age: 19 }]

Prod & Output:

prod=> \d test_user

 Column |       Type        | Collation | Nullable | Default 
--------+-------------------+-----------+----------+---------
 id     | integer           |           | not null | 
 age    | integer           |           | not null | 
 name   | character varying |           | not null | 
[User { id: 1, name: "\0\0\0\u{13}", age: 1952805748 }]
@liutaon liutaon added the bug label Mar 3, 2024
@abonander
Copy link
Collaborator

This is already covered in the documentation improvements added by #3007

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants