Django MySQL | How to remove foreign key column and table from an existing table to do migration again?
x/** List foreign key ( NOTE that its different from index name ) */
mysql> SHOW CREATE TABLE myapp;
/* output
myapp | CREATE TABLE `myapp` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`notes` longtext,
`device_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `device_id` (`device_id`),
CONSTRAINT `myapp_device_id_867b219a_fk_otherapp_device_id` FOREIGN KEY (`device_id`) REFERENCES `otherapp_device` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1085 DEFAULT CHARSET=latin1 |
1 row in set (0,00 sec)
*/
/****************** NOTE ************ */
/* grab CONSTRAINT `AUTO_GENERATED_FOREIGN_KEY_COLUMN_NAME`. In our case that is myapp_device_id_867b219a_fk_otherapp_device_id */
mysql> ALTER TABLE frontend_meter DROP FOREIGN KEY `myapp_device_id_867b219a_fk_otherapp_device_id`;
/*
Query OK, 0 rows affected (0,01 sec)
Records: 0 Duplicates: 0 Warnings: 0
*/
/** Now we can drop that column */
mysql> ALTER TABLE myapp DROP COLUMN device_id;
- Now we can drop table and migrate the table again
xxxxxxxxxx
mysql> DROP TABLE otherapp_device;
Query OK, 0 rows affected (0,02 sec)
- Python Django
$ python manage.py migrate myapp
Yorumlar