launchbadge / sqlx

🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, and SQLite.
Apache License 2.0
12.36k stars 1.18k forks source link

Different MYSQL and MARIADB drivers #3317

Closed joao-conde closed 4 days ago

joao-conde commented 4 days ago

Is your feature request related to a problem? Please describe.

MYSQL and MARIADB are alike and the latter is supposed to be a drop-in replacement for the first, but not the other way around. By using the same driver for both mysql and mariadb users, we are limiting our ability to use some mariadb specific features.

Im frustrated by not being able to use features like INSERT ... RETURNING *.

Describe the solution you'd like

Create a very similar driver for mariadb and add the new capabilities, starting with RETURNING :p

Describe alternatives you've considered

Swapping databases or performing an extra query to fetch the result, both unwanted.

joao-conde commented 4 days ago

I'm willing to pick this up if agreed upon.

abonander commented 4 days ago

There's nothing stopping you from using MariaDB features through the MySQL driver. If your application is trying to support both, I could see adding a flag or a getter to MySqlConnection to return whether it's connected to a MySQL or a MariaDB database.

abonander commented 4 days ago

I don't see how adding a separate driver would make things easier. It'd require duplicate code paths or runtime checks either way.

joao-conde commented 4 days ago

Maybe I assumed to know the issue, and I'm wrong. I should perhaps open a bug one, but for example:

This code works with SQLITE:

pub async fn create_todo(pool: &SqlitePool, todo: CreateTodo) -> Result<Todo, InternalError> {
    let todo = sqlx::query_as!(
        Todo,
        "INSERT INTO todos (title, description) VALUES (?, ?) RETURNING *",
        todo.title,
        todo.description
    )
    .fetch_one(pool)
    .await?;
    Ok(todo)
}

The same code wont work for a MySqlPool. The error is ColumntNotFound("id"). Ive tried explicitly enumerating the return columns too, no luck. This is using mariadb:11.4 and if I execute that same query on a mariadb client or through a terminal connection to the DB I get the correct return.

abonander commented 4 days ago

Are you using MySqlPool connected to a MariaDB database?

joao-conde commented 4 days ago

Yup, docker image for mariadb:11.4

joao-conde commented 4 days ago

If you want ill open a bug issue and we close this one.

abonander commented 4 days ago

Feel free to open a bug.

joao-conde commented 4 days ago

Closing this in favor of https://github.com/launchbadge/sqlx/issues/3318