Skip to content

Incorrect conversion from MySQL UUID string to Rust UUID struct. #2489

Open
@aazev

Description

@aazev

Bug Description

Hello, I'm experiencing an issue with conversion of a UUID field when using the sqlx crate with a MySQL database.

Here's my struct:

#[derive(Debug, Serialize, Deserialize, FromRow)]
pub struct UserModel {
    pub id: u64,
    pub api_token: Uuid,
    pub name: String,
    pub email: String,
    #[serde(skip_serializing)]
    pub password: String,
    pub created_at: DateTime<Utc>,
    pub updated_at: Option<DateTime<Utc>>,
}

As you can see, api_token is of type Uuid (doesn't matter if uuid::Uuid or sqlx::types::Uuid). In MySQL, this column is stored as a string.
When retrieved using the fetch_all() method, the resulting JSON contains the same api_token value for all entries, even though they're unique in the database.

[
    {
        "id": 1,
        "api_token": "6170695f-746f-6b65-6e21-3a2055756964",
        "name": "Naruto Uzumaki",
        "email": "ramenlover_naruto@leafvillage.com",
        "created_at": "2023-05-04T00:39:12Z",
        "updated_at": null
    },
    {
        "id": 2,
        "api_token": "6170695f-746f-6b65-6e21-3a2055756964",
        "name": "Sasuke Uchiha",
        "email": "emoavenger_sasuke@shariganshadow.net",
        "created_at": "2023-05-04T01:27:34Z",
        "updated_at": null
    }
]

When changing the api_token field in my struct to String, the resulting JSON reflects the database records:

[
    {
        "id": 1,
        "api_token": "6170695f-746f-6b65-6e21-3a2055756964",
        "name": "Naruto Uzumaki",
        "email": "ramenlover_naruto@leafvillage.com",
        "created_at": "2023-05-04T00:39:12Z",
        "updated_at": null
    },
    {
        "id": 2,
        "api_token": "42387d54-8b93-4e5d-8e49-799f426d1194",
        "name": "Sasuke Uchiha",
        "email": "emoavenger_sasuke@shariganshadow.net",
        "created_at": "2023-05-04T01:27:34Z",
        "updated_at": null
    }
]

Please let me know if you need any further information or if I can assist with this issue in any way.

Thank you for your attention to this matter.

Minimal Reproduction

A small code snippet or a link to a Github repo or Gist, with instructions on reproducing the bug.
I've created a repo which replicates the issue, despite using a different struct:

#[derive(Debug, FromRow)]
pub struct UserModel {
    pub id: i32,
    pub api_token: Uuid,
    pub name: String,
    pub email: String,
}

Info

  • SQLx version: 0.6.3
  • SQLx features enabled: [
    "bigdecimal",
    "macros",
    "runtime-tokio-rustls",
    "json",
    "chrono",
    "decimal",
    "uuid",
    "mysql",
    "migrate",
    "tls",
    "offline",
    "ipnetwork",
    ]
  • uuid version: 1.3.2
  • Database server and version: MySql 8.0
  • Operating system: Ubuntu 22.04
  • rustc --version: rustc 1.69.0 (84c898d65 2023-04-16)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugdocumentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions