将 m2m 更改为 char 时,Django South 迁移失败
Posted
技术标签:
【中文标题】将 m2m 更改为 char 时,Django South 迁移失败【英文标题】:Django South migration fails when changing m2m to char 【发布时间】:2013-02-13 11:13:59 【问题描述】:我有一个现有的 Django 项目,之前成功迁移了 25 次。
我将模型中的一些字段更改为不同的类型,例如将 m2m 字段更改为文本字段,我现在正在尝试迁移它。
我跑了:
python manage.py schemamigration backend --auto
然后
python manage.py migrate
我得到以下信息
后端:0026_auto__del_vehicle__del_assetproperty__del_liabilityproperty__del_banka 致命错误 - 以下 SQL 查询失败:ALTER TABLE
backend_userprofile
MODIFYprevious_address
longtext NULL;; 错误是:(1170,“在没有密钥长度的密钥规范中使用了 BLOB/TEXT 列 'previous_address'”) !在实际运行迁移过程中发现错误!中止。
! Since you have a database that does not support running
! schema-altering statements in transactions, we have had
! to leave it in an interim state between migrations.
! You *might* be able to recover with: = CREATE INDEX `backend_userprofile_a44d8d16` ON `backend_userprofile` (`employer_address_id`); []
= CREATE INDEX `backend_userprofile_eb751626` ON `backend_userprofile` (`previous_address_id`); []
= CREATE INDEX `backend_userprofile_174f2184` ON `backend_userprofile` (`employer_phone_id`); []
= CREATE TABLE `backend_vehicle` (`make` varchar(30) NOT NULL, `financial_status` integer NOT NULL, `values` numeric(30, 2) NOT NULL, `year` date NOT NULL, `model` varchar(30) NOT NULL, `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY); []
= CREATE TABLE `backend_assetproperty` (`registered_name` varchar(30) NOT NULL, `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `estimate_value` numeric(30, 2) NOT NULL, `purchase_price` numeric(30, 2) NOT NULL); []
= CREATE TABLE `backend_liabilityproperty` (`taxes_included` bool NOT NULL, `insured` bool NOT NULL, `monthly_payment` numeric(30, 2) NOT NULL, `payment_type` integer NOT NULL, `landlord` varchar(30) NOT NULL, `property_taxes` numeric(30, 2) NOT NULL, `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `amount_owing` numeric(30, 2) NOT NULL); []
= CREATE TABLE `backend_bankaccountasset` (`balance` numeric(30, 2) NOT NULL, `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `bank_name` varchar(30) NOT NULL); []
= CREATE TABLE `backend_credit` (`paid_in_full` bool NOT NULL, `monthly_payment` numeric(30, 2) NOT NULL, `person_business` integer NOT NULL, `balance` numeric(30, 2) NOT NULL, `institution_name` varchar(30) NOT NULL, `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `credit_limit` numeric(30, 2) NOT NULL); []
= DROP TABLE `backend_liability` CASCADE; []
= DROP TABLE `backend_asset` CASCADE; []
= DROP TABLE `backend_otherincome` CASCADE; []
= ALTER TABLE `backend_userprofile` ADD COLUMN `bank_branch_number` integer NULL; []
- no dry run output for alter_column() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` ADD COLUMN `address_id` integer NULL; []
- no dry run output for alter_column() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` ADD COLUMN `gross_monthly_income` numeric(30, 2) NULL; []
- no dry run output for alter_column() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` ADD COLUMN `other_income` numeric(30, 2) NULL; []
- no dry run output for alter_column() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` ADD COLUMN `bank_phone_id` integer NULL; []
- no dry run output for alter_column() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` ADD COLUMN `bank_contact_details_id` integer NULL; []
- no dry run output for alter_column() due to dynamic DDL, sorry
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `current_address` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `current_unit_number` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `current_city` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `current_province` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `current_postal_code` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `current_time_at_residence` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `previous_unit_number` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `previous_city` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `previous_province` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `previous_postal_code` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `employment_position` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `employmer_manager_name` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `employment_duration_years` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `employment_duration_months` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `employer_unit_number` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `employer_city` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `employer_province` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `employer_postal_code` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `previous_employment_type` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `previous_employer_phone` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `previous_employer_address` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `previous_employer_unit_number` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `previous_employer_city` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `previous_employer_province` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `previous_employer_postal_code` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `employment_monthly_income` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `bank_contact_phone` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `bank_contact_fax` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `bank_institution_number` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `bank_transit_number` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `bank_address` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `bank_unit_number` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `bank_city` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `bank_province` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `bank_postal_code` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `residence_type` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `residence_value` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `residence_monthly_payment` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `claimed_bankruptcy` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `fraud_alerts` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `credit_consent` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `notes` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `signature` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `cobuyer_relationship` CASCADE; []
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
= ALTER TABLE `backend_userprofile` DROP COLUMN `cobuyer_other` CASCADE; []
= CREATE TABLE `backend_userprofile_vehicles` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `userprofile_id` integer NOT NULL, `vehicle_id` integer NOT NULL); []
= ALTER TABLE `backend_userprofile_vehicles` ADD CONSTRAINT `backend_userprofile_vehicl_userprofile_id_2118f7c8e02a7a01_uniq` UNIQUE (`userprofile_id`, `vehicle_id`) []
= CREATE TABLE `backend_userprofile_bank_accounts_asset` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `userprofile_id` integer NOT NULL, `bankaccountasset_id` integer NOT NULL); []
= ALTER TABLE `backend_userprofile_bank_accounts_asset` ADD CONSTRAINT `backend_userprofile_bank_a_userprofile_id_3780e7dea36fdd1b_uniq` UNIQUE (`userprofile_id`, `bankaccountasset_id`) []
= CREATE TABLE `backend_userprofile_credit` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `userprofile_id` integer NOT NULL, `credit_id` integer NOT NULL); []
= ALTER TABLE `backend_userprofile_credit` ADD CONSTRAINT `backend_userprofile_credit_userprofile_id_aa5c94a808f9fb7_uniq` UNIQUE (`userprofile_id`, `credit_id`) []
= CREATE TABLE `backend_userprofile_property_asset` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `userprofile_id` integer NOT NULL, `assetproperty_id` integer NOT NULL); []
= ALTER TABLE `backend_userprofile_property_asset` ADD CONSTRAINT `backend_userprofile_proper_userprofile_id_3397e38f7c83f4db_uniq` UNIQUE (`userprofile_id`, `assetproperty_id`) []
= CREATE TABLE `backend_userprofile_liability_properties` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `userprofile_id` integer NOT NULL, `liabilityproperty_id` integer NOT NULL); []
= ALTER TABLE `backend_userprofile_liability_properties` ADD CONSTRAINT `backend_userprofile_liabil_userprofile_id_21457cbf6edd1007_uniq` UNIQUE (`userprofile_id`, `liabilityproperty_id`) []
= DROP TABLE `backend_userprofile_other_income` CASCADE; []
= DROP TABLE `backend_userprofile_assets` CASCADE; []
= DROP TABLE `backend_userprofile_liabilities` CASCADE; []
=
SELECT kc.`constraint_name`, kc.`column_name`, kc.`table_name`,
kc.`referenced_table_name`, kc.`referenced_column_name`
FROM information_schema.key_column_usage AS kc
WHERE
kc.table_schema = %s
['pomelo_walker']
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
- no dry run output for alter_column() due to dynamic DDL, sorry
=
SELECT kc.`constraint_name`, kc.`column_name`, kc.`table_name`,
kc.`referenced_table_name`, kc.`referenced_column_name`
FROM information_schema.key_column_usage AS kc
WHERE
kc.table_schema = %s
['pomelo_walker']
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
- no dry run output for alter_column() due to dynamic DDL, sorry
=
SELECT kc.`constraint_name`, kc.`column_name`, kc.`table_name`,
kc.`referenced_table_name`, kc.`referenced_column_name`
FROM information_schema.key_column_usage AS kc
WHERE
kc.table_schema = %s
['pomelo_walker']
- no dry run output for delete_foreign_key() due to dynamic DDL, sorry
- no dry run output for alter_column() due to dynamic DDL, sorry
! The South developers regret this has happened, and would
! like to gently persuade you to consider a slightly
! easier-to-deal-with DBMS (one that supports DDL transactions)
! NOTE: The error which caused the migration to fail is further up.
Error in migration: backend:0026_auto__del_vehicle__del_assetproperty__del_liabilityproperty__del_banka
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/chungwu/.virtualenvs/walkerfinancial/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/Users/chungwu/.virtualenvs/walkerfinancial/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/chungwu/.virtualenvs/walkerfinancial/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Users/chungwu/.virtualenvs/walkerfinancial/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/Users/chungwu/.virtualenvs/walkerfinancial/lib/python2.7/site-packages/south/management/commands/migrate.py", line 108, in handle
ignore_ghosts = ignore_ghosts,
File "/Users/chungwu/.virtualenvs/walkerfinancial/lib/python2.7/site-packages/south/migration/__init__.py", line 213, in migrate_app
success = migrator.migrate_many(target, workplan, database)
File "/Users/chungwu/.virtualenvs/walkerfinancial/lib/python2.7/site-packages/south/migration/migrators.py", line 235, in migrate_many
result = migrator.__class__.migrate_many(migrator, target, migrations, database)
File "/Users/chungwu/.virtualenvs/walkerfinancial/lib/python2.7/site-packages/south/migration/migrators.py", line 310, in migrate_many
result = self.migrate(migration, database)
File "/Users/chungwu/.virtualenvs/walkerfinancial/lib/python2.7/site-packages/south/migration/migrators.py", line 133, in migrate
result = self.run(migration)
File "/Users/chungwu/.virtualenvs/walkerfinancial/lib/python2.7/site-packages/south/migration/migrators.py", line 107, in run
return self.run_migration(migration)
File "/Users/chungwu/.virtualenvs/walkerfinancial/lib/python2.7/site-packages/south/migration/migrators.py", line 81, in run_migration
migration_function()
File "/Users/chungwu/.virtualenvs/walkerfinancial/lib/python2.7/site-packages/south/migration/migrators.py", line 57, in <lambda>
return (lambda: direction(orm))
File "/Users/chungwu/Sites/pomelo/walkerfinancial/backend/migrations/0026_auto__del_vehicle__del_assetproperty__del_liabilityproperty__del_banka.py", line 347, in forwards
db.alter_column('backend_userprofile', 'previous_address', self.gf('django.db.models.fields.TextField')(null=True))
File "/Users/chungwu/.virtualenvs/walkerfinancial/lib/python2.7/site-packages/south/db/generic.py", line 44, in _cache_clear
return func(self, table, *args, **opts)
File "/Users/chungwu/.virtualenvs/walkerfinancial/lib/python2.7/site-packages/south/db/generic.py", line 527, in alter_column
self.execute("ALTER TABLE %s %s;" % (self.quote_name(table_name), sql), values)
File "/Users/chungwu/.virtualenvs/walkerfinancial/lib/python2.7/site-packages/south/db/generic.py", line 273, in execute
cursor.execute(sql, params)
File "/Users/chungwu/.virtualenvs/walkerfinancial/lib/python2.7/site-packages/django/db/backends/util.py", line 40, in execute
return self.cursor.execute(sql, params)
File "/Users/chungwu/.virtualenvs/walkerfinancial/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 114, in execute
return self.cursor.execute(query, args)
File "/Users/chungwu/.virtualenvs/walkerfinancial/lib/python2.7/site-packages/MySQLdb/cursors.py", line 201, in execute
self.errorhandler(self, exc, value)
File "/Users/chungwu/.virtualenvs/walkerfinancial/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.DatabaseError: (1170, "BLOB/TEXT column 'previous_address' used in key specification without a key length")
有什么办法可以解决这个问题吗?
我正在运行 Django 1.4.5 和 South 0.7.6
【问题讨论】:
您使用的是什么 DBMS? 【参考方案1】:首先从您应用中的迁移文件夹中删除您上次的迁移。
然后我建议删除 m2m 字段,创建迁移,然后添加新的文本字段并创建另一个迁移。如果您分两步进行,我认为 South 应该能够处理此过程。
【讨论】:
以上是关于将 m2m 更改为 char 时,Django South 迁移失败的主要内容,如果未能解决你的问题,请参考以下文章
Django - 将 ForeignKey 关系更改为 OneToOne