当我在我的某个模型中加入ModelToModelField
时,会抛出以下错误。
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_manager(settings)
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 362, in execute_manager
utility.execute()
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 303, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 195, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 222, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 351, in handle
return self.handle_noargs(**options)
File "/Library/Python/2.6/site-packages/django/core/management/commands/syncdb.py", line 93, in handle_noargs
cursor.execute(statement)
File "/Library/Python/2.6/site-packages/django/db/backends/util.py", line 19, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.6/site-packages/django/db/backends/mysql/base.py", line 84, in execute
return self.cursor.execute(query, args)
File "build/bdist.macosx-10.6-universal/egg/MySQLdb/cursors.py", line 173, in execute
File "build/bdist.macosx-10.6-universal/egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
_mysql_exceptions.OperationalError: (1050, "Table 'orders_proof_approved_associations' already exists")
字段定义:
proof_storage = FileSystemStorage(location=settings.FILE_UPLOAD_ROOT)
class Proof(mixins.ModifiedDates):
"""
Each Order eventually has a Proof or multiple rounds of Proofs. Required are a proof file and
a Record Set file containing the records used for the proof.
"""
def get_upload_path(instance, filename):
proof_count = int(Proof.objects.filter(order=instance.order).count()) + 1
destination_path = os.path.join(instance.order.ATTACHMENTS_RELATIVE_ROOT, 'proofs')
return os.path.join(destination_path, '%02d_%s' % (proof_count, filename))
file = models.FileField(storage=proof_storage, upload_to=get_upload_path)
approved = models.BooleanField(default=0)
order = models.ForeignKey(Order)
record_set_file = models.FileField(storage=proof_storage, upload_to=get_upload_path)
approved_associations = models.ManyToManyField(Association)
当我移除田地时,一切正常,而且桌子不在眼前。
有关为何会发生这种情况的任何想法?
答案 0 :(得分:0)
您始终可以将db_table
选项传递给ManyToManyField,以便绕过Django创建/使用的默认表名。请参阅documentation here。
但是,您不应该看到这样的错误。如果您可以发布模型代码,我们可以提供更好的帮助。
答案 1 :(得分:0)
努力让django做我想做的事。
我有两个带有通用名称的应用。显然是一个很大的禁忌,但我让他们组织成子应用subapp1.appname
和subapp2.appname
。无论如何,当它归结为它时,Django只关心appname *,当我运行syncdb时,它遇到了INSTALLED_APPS
设置中的两个应用程序,并试图安装m2m模型两次。无论如何,通过重命名其中一个应用程序来解决(足够有趣到一个更合适的名称)。
* In Django 1.2 there is better support for `app_labels`
which can be used instead of the app name when referencing.