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

SQLx CLI error: Using @variable in WHERE statement causes "Illegal mix of collations" #3200

Open
LucHayward opened this issue Apr 16, 2024 · 0 comments
Labels

Comments

@LucHayward
Copy link

Bug Description

When using the sqlx CLI to run migrations on MariaDB, using variables in where statements results in a mixed collation error.
This does not occur if you run the sql queries "manually".

Using the minimal example below you will see:
error: while executing migrations: error returned from database: 1267 (HY000): Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_unicode_ci,IMPLICIT) for operation '='

This can be resolved by forcing the collation to be utf8mb4_unicode_ci for the given statement.

Minimal Reproduction

Place these two files into ./migrations/
1_collate_example.up.sql

CREATE TABLE users
(
    `id`                     BIGINT AUTO_INCREMENT,
    `username`               VARCHAR(128) NOT NULL,
    PRIMARY KEY (id)
);

2_collate_example.up.sql

SET @myvar := 'test@test.com';
select id from users where username = @myvar;

Assuming you have a mysql/MariaDB server setup with a user, the following will create and run our migrations.

export DATABASE_URL=mysql://zulzi:zulzi@localhost/collate-example
sqlx database drop -y
sqlx database create
sqlx migrate run

A working example for 2_collate_example.up.sql would be

SET @myvar := 'test@test.com';
select id from users where username = @myvar COLLATE utf8mb4_unicode_ci;

OR

select id from users where username = 'test@test.com';

Info

  • SQLx version: sqlx-cli 0.7.4
  • SQLx features enabled: Don't think this applies
  • Database server and version: mysql from 11.3.2-MariaDB, client 15.2 for osx10.19 (arm64) using EditLine wrapper
  • Operating system: macOS 14.4.1 (23E224)
  • rustc --version: rustc 1.77.1 (7cf61ebde 2024-03-27)
@LucHayward LucHayward added the bug label Apr 16, 2024
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

1 participant