Opened 14 years ago
Closed 13 years ago
#15933 closed Bug (fixed)
mysql inspectdb loosing primary_key information
Reported by: | andi | Owned by: | Anssi Kääriäinen |
---|---|---|---|
Component: | Core (Management commands) | Version: | 1.3 |
Severity: | Normal | Keywords: | inspectdb mysql |
Cc: | andi | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | yes | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I've been inspecting a mysql 4 (4.0.16-standard) database. The resulting models file didn't handle all the primary_keys right if the primary key column is used in any other key.
Example table layout:
CREATE TABLE `test` ( `PrimaryKeyColumn` int(10), `AnotherColumn` int(10), `SomethingElse` int(10), PRIMARY KEY `PrimaryKeyColumn`, UNIQUE KEY `AnotherKey` (`PrimaryKeyColumn`,`AnotherColumn`) );
The SHOW INDEX FROM %s
query would return the following table:
mysql> show index from test; +-------+------------+------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | +-------+------------+------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+ | test | 0 | PRIMARY | 1 | PrimaryKeyColumn | A | 0 | NULL | NULL | | BTREE | | | test | 0 | AnotherKey | 1 | PrimaryKeyColumn | A | NULL | NULL | NULL | | BTREE | | | test | 0 | AnotherKey | 2 | AnotherColumn | A | NULL | NULL | NULL | YES | BTREE | | +-------+------------+------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+
After reading row 2 the primary_key information would be lost.
To preserve this information i added a check to only update the unique
field. Patch is attached.
I had to apply #14618 to use inspectdb with my mysql version.
Attachments (1)
Change History (10)
by , 14 years ago
Attachment: | django_mysql4_multiple_keys_keep_primary.diff added |
---|
comment:1 by , 14 years ago
Cc: | added |
---|
comment:2 by , 14 years ago
Needs tests: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 14 years ago
comment:5 by , 13 years ago
Component: | Database layer (models, ORM) → Core (Management commands) |
---|---|
Keywords: | inspectdb added |
UI/UX: | unset |
comment:6 by , 13 years ago
Keywords: | mysql4 added |
---|
comment:7 by , 13 years ago
Keywords: | mysql added; mysql4 removed |
---|
Was able to reproduce on MySQL 5.1
comment:8 by , 13 years ago
Owner: | changed from | to
---|
If I understand correctly only single field indexes should be inspected by Django.
PostgreSQL deals with this issue by skipping all multi-column indexes. The intention is to do the same for Oracle. Oracle has a very similar bug (#18082). So, I think MySQL should skip multi-column indexes, too.
I am assigning this bug to myself. My intention is to unify the get_indexes methods across all backends to inspect only single-column indexes.
comment:9 by , 13 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
I've been thinking about this patch over the weekend. The provided patch is ok for primary keys. If checking for uniqueness a multi-field unique key would mark all involved fields as unique. A single field-unique key is detected correct.
I've written the following code to detect single/multi-field unique keys correctly:
I've hacked a couple of test cases together in a test-django-project to verify the unique and primary fields. Is there any central location within django where db related tests should be ?
Also it seems that there is currently no way to pass the information of multi-field unique keys to the inspectdb management command?