MySQL character set error

django.db.utils.OperationalError: (1267, "Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='")

The above error is obviously caused by character set matching. So log in to MySQL to modify the relevant configuration.
First check the MySQL database configuration:

mysql> show variables like '%coll%';
+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | latin1_swedish_ci    |
| collation_database   | latin1_swedish_ci |
| collation_server     | latin1_swedish_ci   |
+----------------------+--------------------+

Modify the database configuration to utf8 character set:

SET collation_connection = utf8_general_ci;
SET collation_database = utf8_general_ci;
SET collation_server = utf8_general_ci;

But the error has not changed after accessing the service, then check the character set of the table

mysql> show create table test;
+----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table          | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
+----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test | CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(200) CHARACTER SET latin1 NOT NULL,
  `depth` int(11) NOT NULL,
  `url` varchar(200) CHARACTER SET latin1 NOT NULL,
  `creater_id` int(11) NOT NULL,
  `parent_id` int(11) DEFAULT NULL,
  `app` varchar(20) CHARACTER SET latin1 NOT NULL,
  `createtime` datetime(6) NOT NULL,
  `operatetime` datetime(6) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `test_name_parent_id_96346d13_uniq` (`name`,`parent_id`),
  KEY `test_creater_id_0825cbdf_fk_auth_user_id` (`creater_id`),
  KEY `test_parent_id_03eda650_fk_test_id` (`parent_id`),
  CONSTRAINT `test_creater_id_0825cbdf_fk_auth_user_id` FOREIGN KEY (`creater_id`) REFERENCES `auth_user` (`id`),
  CONSTRAINT `test_parent_id_03eda650_fk_test_id` FOREIGN KEY (`parent_id`) REFERENCES `test` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
+----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Seeing that the character set of the table is already utf8mb4, then check the configuration of each field:

mysql> show full columns from dataset_folder;
+-------------+--------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+
| Field       | Type         | Collation         | Null | Key | Default | Extra          | Privileges                      | Comment |
+-------------+--------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+
| id          | int(11)      | NULL              | NO   | PRI | NULL    | auto_increment | select,insert,update,references |         |
| name        | varchar(200) | latin1_swedish_ci | NO   | MUL | NULL    |                | select,insert,update,references |         |
| depth       | int(11)      | NULL              | NO   |     | NULL    |                | select,insert,update,references |         |
| url         | varchar(200) | latin1_swedish_ci | NO   |     | NULL    |                | select,insert,update,references |         |
| creater_id  | int(11)      | NULL              | NO   | MUL | NULL    |                | select,insert,update,references |         |
| parent_id   | int(11)      | NULL              | YES  | MUL | NULL    |                | select,insert,update,references |         |
| app         | varchar(20)  | latin1_swedish_ci | NO   |     | NULL    |                | select,insert,update,references |         |
| createtime  | datetime(6)  | NULL              | NO   |     | NULL    |                | select,insert,update,references |         |
| operatetime | datetime(6)  | NULL              | NO   |     | NULL    |                | select,insert,update,references |         |
+-------------+--------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+

When you see the characters in the field latin1_swedish_ci, turn to utf8:

mysql> alter table dataset_folder convert to character set utf8mb4 collate utf8mb4_general_ci;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show full columns from dataset_folder;                                                                                                                                                               +-------------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+---------+
| Field       | Type         | Collation          | Null | Key | Default | Extra          | Privileges                      | Comment |
+-------------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+---------+
| id          | int(11)      | NULL               | NO   | PRI | NULL    | auto_increment | select,insert,update,references |         |
| name        | varchar(200) | utf8mb4_general_ci | NO   | MUL | NULL    |                | select,insert,update,references |         |
| depth       | int(11)      | NULL               | NO   |     | NULL    |                | select,insert,update,references |         |
| url         | varchar(200) | utf8mb4_general_ci | NO   |     | NULL    |                | select,insert,update,references |         |
| creater_id  | int(11)      | NULL               | NO   | MUL | NULL    |                | select,insert,update,references |         |
| parent_id   | int(11)      | NULL               | YES  | MUL | NULL    |                | select,insert,update,references |         |
| app         | varchar(20)  | utf8mb4_general_ci | NO   |     | NULL    |                | select,insert,update,references |         |
| createtime  | datetime(6)  | NULL               | NO   |     | NULL    |                | select,insert,update,references |         |
| operatetime | datetime(6)  | NULL               | NO   |     | NULL    |                | select,insert,update,references |         |
+-------------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+---------+
9 rows in set (0.00 sec)

You can see that the configuration of the field has also been modified.
Visit the service again and return data successfully.

Guess you like

Origin blog.csdn.net/JosephThatwho/article/details/106389428