2016-01-09 76 views
8

我試圖運行我的Django項目遷移,但我得到的錯誤:「ManyToManyField」對象有沒有屬性「m2m_reverse_field_name」

AttributeError: 'ManyToManyField' object has no attribute 'm2m_reverse_field_name' 

我,當我跑就遷移我所有的應用程序,我沒有得到任何錯誤。只有當我嘗試實際遷移時。我無法從追溯信息中知道哪個模型正在創建問題,甚至哪個應用程序。我看過我的模型,但沒有看到任何突然出現在我身上的東西。

這裏是堆棧跟蹤:

Operations to perform: 
    Apply all migrations: admin, sessions, case_manager, file_manager, auth, contenttypes, tasks, people_and_property 
Running migrations: 
    Rendering model states... DONE 
    Applying file_manager.0006_auto_20160109_1536...Traceback (most recent call last): 
    File "manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
    File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line 
    utility.execute() 
    File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 342, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute 
    output = self.handle(*args, **options) 
    File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 200, in handle 
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial) 
    File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 92, in migrate 
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial) 
    File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards 
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) 
    File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 198, in apply_migration 
    state = migration.apply(state, schema_editor) 
    File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/migrations/migration.py", line 123, in apply 
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state) 
    File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards 
    schema_editor.alter_field(from_model, from_field, to_field) 
    File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 467, in alter_field 
    return self._alter_many_to_many(model, old_field, new_field, strict) 
    File "/home/mint/Python_Projects/venv/lib/python3.4/site-packages/django/db/backends/sqlite3/schema.py", line 274, in _alter_many_to_many 
    old_field.remote_field.through._meta.get_field(old_field.m2m_reverse_field_name()), 
AttributeError: 'ManyToManyField' object has no attribute 'm2m_reverse_field_name' 

我如何找出哪些型號是什麼問題?我應該尋找什麼?

+0

在某些模型發生更改後,您會收到此錯誤消息嗎? –

+0

我做了一些小改動。我實際上已經嘗試回去改變他們的方式,但這似乎沒有什麼區別。我沒有做任何奇特的事情,字面意義上ManytoMany字段與related_name。 – jejy2343

+0

你可以顯示完整的字段定義嗎? –

回答

4

我遇到了同樣的問題,但我不知道是否出於同樣的原因。幸運的是,我沒有系統中的重要數據,所以我只是將遷移更改爲 - ,但請注意,這會刪除這些列中的所有數據!

前:

operations = [ 
    migrations.AlterField(
     model_name='resource', 
     name='authors', 
     field=models.ManyToManyField(related_name='resources_authored', to='api.Person'), 
    ), 
    migrations.AlterField(
     model_name='resource', 
     name='editors', 
     field=models.ManyToManyField(blank=True, related_name='resources_edited', to='api.Person'), 
    ), 
] 

後:

operations = [ 
    migrations.RemoveField(
     model_name='resource', 
     name='authors', 
    ), 
    migrations.RemoveField(
     model_name='resource', 
     name='editors', 
    ), 
    migrations.AddField(
     model_name='resource', 
     name='authors', 
     field=models.ManyToManyField(related_name='resources_authored', to='api.Person'), 
    ), 
    migrations.AddField(
     model_name='resource', 
     name='editors', 
     field=models.ManyToManyField(blank=True, related_name='resources_edited', to='api.Person'), 
    ), 
] 

雖然改變失敗原因的神祕,刪除並重新創建下地幹活。

+0

這不會刪除所有可能存儲在這些字段中的數據嗎? – Brachamul

+0

@Brachamul,贊成。這就是我的意思,「幸運的是,我沒有系統中的重要數據,所以我只是改變了遷移,如下所示。」我將編輯並做出更清晰的說明。 –

+0

@Sagar和jsep的以下答案更有用 – TauPan

2

其中一個原因可能是您的api.Person模型遷移可能沒有運行之前引用它(在您的示例中它是file_manager.0006_auto_20160109_1536)。因此,確保api.Person遷移是在file_manager.0006_auto_20160109_1536的依賴關係中添加它們之前運行的。

+0

這就是爲我造成並解決它的原因。我添加了依賴到像這樣的遷移:'依賴關係= [...,('api','__first__'),]'。 之後,AlterField工作。 – Brachamul

+0

@Brachamul也許你應該接受這個答案,然後呢? – TauPan

+0

我不是那個問這個問題的人,所以我不能接受答案? – Brachamul

0

當我嘗試重命名一個被許多字段引用的表時,我遇到了同樣的問題。 我解決了這種方式: - 甩了多對多關係的數據到一個文件 - 刪除的多對多場和遷移 - 重命名錶和遷移 - 增加了多對多外地趕回並遷移並加載從轉儲的關係

6

您必須確保您創建'ManyToManyField'的模型已經在數據庫中創建。

你可以通過添加作爲一個依賴在模型中創建您的遷移中,你改變了場遷移:

方案1:您與其他模型改變現場「ManyToManyField」應用

class Migration(migrations.Migration): 

    dependencies = [ 
     .......... 
     ('[app]', '__first__'), 
    ] 

    operations = [ 
     ......... 
    ] 

方案2:創建一個「ManyToManyField」,你指的是在同一個文件的模式:

class Migration(migrations.Migration): 

    dependencies = [ 
     .......... 
    ] 

    operations = [ 
     ......... 
     # Make sure the model you are making the reference with is before the ManyToManyField 
     migrations.CreateModel(...) , 
     migrations.AlterField/CreateField(...) 

    ] 
0

我也有相同的p roblem,但我固定的方式,是下列步驟: 諾塔:你會從ManyToManyField丟失數據

python manage.py makemigrations app_name 
python manage.py migrate app_name --fake 

不要忘了--fake

之後。 我刪除(或只是註釋行)ManyToMany字段,然後makemigrations app_name,遷移app_name。

在這一步你清除了你的數據庫中的這一列,你現在需要做的是重新添加ManyToManyField,所以當你進行遷移時,app_name &再次遷移app_name。 你的服務器可以完美運行

這不是最好的,但它爲我工作。 希望它會幫助!