在 Django 中更改 ManyToMany 字段时,我是否破坏了我的数据库?
Did I break my DB while changing ManyToMany field in Django?
所以我有一个 ForeignKey 字段,我需要将它转换为 ManyToManyField。当我 运行 迁移时,它给出了这个错误:
ValueError: Cannot alter field core_app.IndieTrack.contributors into
core_app.IndieTrack.contributors - they are not compatible types (you
cannot alter to or from M2M fields, or add or remove through= on M2M
fields)
所以我说,"fine," 然后我将它还原为 ForeignKey,我想我会想出一个替代解决方案。但是当我再次迁移(回到原来的状态)时,它给了我同样的错误。
现在我根本无法迁移任何更改。无论我做什么,我都会得到同样的错误。
这是整个回溯:
python3.4 manage.py makemigrations core_app Migrations for 'core_app':
0034_auto_20150521_0740.py:
- Create model UserTrack
- Remove field contributors from indietrack
- Delete model IndieTrack
- Alter field tracks on album
python3.4 manage.py migrate
Operations to perform:
Apply all migrations: sessions, home, auth, admin, ipn, beat_store, blog, core _app, contenttypes
Running migrations:
Applying core_app.0024_auto_20150521_0634...Traceback (most recent call last):
Running migrations:
Applying core_app.0024_auto_20150521_0634...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/management/base.py", line 533, in handle
return self.handle_noargs(**options)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/management/commands/syncdb.py", line 27, in handle_noargs
call_command("migrate", **options)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/management/__init__.py", line 115, in call_command
return klass.execute(*args, **defaults)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/management/commands/migrate.py", line 161, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/db/migrations/executor.py", line 68, in migrate
self.apply_migration(migration, fake=fake)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/db/migrations/executor.py", line 102, in apply_migration
migration.apply(project_state, schema_editor)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/db/migrations/migration.py", line 108, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/db/migrations/operations/fields.py", line 139, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/db/backends/schema.py", line 454, in alter_field
new_field,
ValueError: Cannot alter field core_app.IndieTrack.contributors into core_app.IndieTrack.contributors - they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields)
这是我的模型:
class IndieTrack(models.Model):
duration = models.CharField(max_length=12, default='0:00')
track_title = models.CharField(max_length=95)
track_number = models.IntegerField(default=0)
contributors = models.ForeignKey(Artist, null=True, blank=True)
extra_info = models.TextField(null=True, blank=True)
def __str__(self):
return self.track_title
我需要进行更改。任何见解将不胜感激。
我正在使用 PostgreSQL
如果您仅更改了该字段,我建议您返回迁移文件夹并删除在尝试将其更改为 ManytoMany 字段后创建的那些。由于您已经创建了上述迁移,并且 Django 运行 按顺序对它们进行了排序,因此它将继续尝试 运行 它,因为您之后进行的迁移取决于它并且该错误将继续存在出现。当这种情况发生时,为了我自己的理智,我就是这样做的。
如果您更改了其他字段,也许您可以输入该迁移文件并删除与该字段相关的位。
所以我有一个 ForeignKey 字段,我需要将它转换为 ManyToManyField。当我 运行 迁移时,它给出了这个错误:
ValueError: Cannot alter field core_app.IndieTrack.contributors into core_app.IndieTrack.contributors - they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields)
所以我说,"fine," 然后我将它还原为 ForeignKey,我想我会想出一个替代解决方案。但是当我再次迁移(回到原来的状态)时,它给了我同样的错误。
现在我根本无法迁移任何更改。无论我做什么,我都会得到同样的错误。
这是整个回溯:
python3.4 manage.py makemigrations core_app Migrations for 'core_app':
0034_auto_20150521_0740.py:
- Create model UserTrack
- Remove field contributors from indietrack
- Delete model IndieTrack
- Alter field tracks on album
python3.4 manage.py migrate
Operations to perform:
Apply all migrations: sessions, home, auth, admin, ipn, beat_store, blog, core _app, contenttypes
Running migrations:
Applying core_app.0024_auto_20150521_0634...Traceback (most recent call last):
Running migrations:
Applying core_app.0024_auto_20150521_0634...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/management/base.py", line 533, in handle
return self.handle_noargs(**options)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/management/commands/syncdb.py", line 27, in handle_noargs
call_command("migrate", **options)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/management/__init__.py", line 115, in call_command
return klass.execute(*args, **defaults)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/core/management/commands/migrate.py", line 161, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/db/migrations/executor.py", line 68, in migrate
self.apply_migration(migration, fake=fake)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/db/migrations/executor.py", line 102, in apply_migration
migration.apply(project_state, schema_editor)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/db/migrations/migration.py", line 108, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/db/migrations/operations/fields.py", line 139, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/home/thephltr/webapps/who_pro/lib/python3.4/Django-1.7.7-py3.4.egg/django/db/backends/schema.py", line 454, in alter_field
new_field,
ValueError: Cannot alter field core_app.IndieTrack.contributors into core_app.IndieTrack.contributors - they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields)
这是我的模型:
class IndieTrack(models.Model):
duration = models.CharField(max_length=12, default='0:00')
track_title = models.CharField(max_length=95)
track_number = models.IntegerField(default=0)
contributors = models.ForeignKey(Artist, null=True, blank=True)
extra_info = models.TextField(null=True, blank=True)
def __str__(self):
return self.track_title
我需要进行更改。任何见解将不胜感激。
我正在使用 PostgreSQL
如果您仅更改了该字段,我建议您返回迁移文件夹并删除在尝试将其更改为 ManytoMany 字段后创建的那些。由于您已经创建了上述迁移,并且 Django 运行 按顺序对它们进行了排序,因此它将继续尝试 运行 它,因为您之后进行的迁移取决于它并且该错误将继续存在出现。当这种情况发生时,为了我自己的理智,我就是这样做的。
如果您更改了其他字段,也许您可以输入该迁移文件并删除与该字段相关的位。