Fix MySQL SqlSchemaDescriber on MySQL 8
In queries, the column names are case-insensitive, but in the ResultSets
we get column names with their original case, which changed in MySQL 8.
Someone had the same problem on Stackoverflow:
@@ -72,7 +72,11 @@ impl SqlSchemaDescriber {
}
fn get_columns(&self, schema: &str, table: &str) -> Vec<Column> {
- let sql = "SELECT column_name, data_type, column_default, is_nullable, extra
+ // We alias all the columns because MySQL column names are case-insensitive in queries, but the
+ // information schema column names became upper-case in MySQL 8, causing the code fetching
+ // the result values by column name below to fail.
+ let sql = "
+ SELECT column_name column_name, data_type data_type, column_default column_default, is_nullable is_nullable, extra extra
FROM information_schema.columns
WHERE table_schema = ? AND table_name = ?
ORDER BY column_name";
@@ -134,8 +138,18 @@ impl SqlSchemaDescriber {
// One should think it's unique since it's used to join information_schema.key_column_usage
// and information_schema.referential_constraints tables in this query lifted from
// Stack Overflow
- let sql = "SELECT kcu.constraint_name, kcu.column_name, kcu.referenced_table_name,
- kcu.referenced_column_name, kcu.ordinal_position, rc.delete_rule
+ //
+ // We alias all the columns because MySQL column names are case-insensitive in queries, but the
+ // information schema column names became upper-case in MySQL 8, causing the code fetching
+ // the result values by column name below to fail.
+ let sql = "
+ SELECT
+ kcu.constraint_name constraint_name,
+ kcu.column_name column_name,
+ kcu.referenced_table_name referenced_table_name,
+ kcu.referenced_column_name referenced_column_name,
+ kcu.ordinal_position ordinal_position,
+ rc.delete_rule delete_rule
FROM information_schema.key_column_usage AS kcu
INNER JOIN information_schema.referential_constraints AS rc ON
kcu.constraint_name = rc.constraint_name
@@ -232,9 +246,15 @@ impl SqlSchemaDescriber {
table_name: &str,
foreign_keys: &[ForeignKey],
) -> (Vec<Index>, Option<PrimaryKey>) {
+ // We alias all the columns because MySQL column names are case-insensitive in queries, but the
+ // information schema column names became upper-case in MySQL 8, causing the code fetching
+ // the result values by column name below to fail.
let sql = "
SELECT DISTINCT
- index_name, non_unique, column_name, seq_in_index
+ index_name AS index_name,
+ non_unique AS non_unique,
+ column_name AS column_name,
+ seq_in_index AS seq_in_index
FROM INFORMATION_SCHEMA.STATISTICS
WHERE table_schema = ? AND table_name = ?
ORDER BY index_name, seq_in_index
