Django syncdb在SQLite中工作,在MySQL中失败 [英] Django syncdb works in SQLite, failing in MySQL

查看:134
本文介绍了Django syncdb在SQLite中工作,在MySQL中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MySQL的新手,并没有在生产中使用Django。我一直在使用SQLite在Windows 7下进行开发,并尝试将代码移动到运行MySQL的Linux服务器。

I'm a newbie to MySQL, and haven't used Django for anything in production. I've been doing development under Windows 7 with SQLite, and am trying to move the code to a Linux server running MySQL.

当我运行syncdb时,它创建了第一个三个表然后抛出一个错误:

When I ran syncdb, it created the first three tables then threw an error:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/gsdemo01/project_file/lib/python3.3/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/home/gsdemo01/project_file/lib/python3.3/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/gsdemo01/project_file/lib/python3.3/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/gsdemo01/project_file/lib/python3.3/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/home/gsdemo01/project_file/lib/python3.3/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/home/gsdemo01/project_file/lib/python3.3/site-packages/django/core/management/commands/syncdb.py", line 96, in handle_noargs
    sql, references = connection.creation.sql_create_model(model, self.style, seen_models)
  File "/home/gsdemo01/project_file/lib/python3.3/site-packages/django/db/backends/creation.py", line 83, in sql_create_model
    model, f, known_models, style)
TypeError: sql_for_inline_foreign_key_references() takes 4 positional arguments but 5 were given

最后一个创建之后的模型是Metaclass,通过继承自它的类:

The model immediately after the last one created is a Metaclass, followed by a class that inherits from it:

class Location(models.Model):
    # con = models.ForeignKey(Convention)
    name = models.CharField(max_length=40)
    scheduler = models.ForeignKey(GCUser, blank=True, null=True)

    class Meta:
        abstract = True

    def __str__(self):
        return self.name


class Room(Location):
    tag = models.CharField(max_length=10)
    capacity = models.PositiveSmallIntegerField(default=100)

,所以似乎这是python连接器包或MySQL本身的一个问题。

This works fine with SQLite, so it seems like it has to be either a problem with the python connector package or with MySQL itself.

提前感谢任何帮助。

更新:

通过尝试和错误我已经确定问题绝对是与外键领域。如果我发表意见,表格创建正确。据我所知,Yogesh建议的修改与代码中已经有的匹配,所以如果有任何其他建议,我很乐意听到他们的意见。

Through trial and error I've determined that the issue is definitely with the Foreign Key field. If I comment that out the table is created correctly. As far as I can tell the modification suggested by Yogesh matches what we already have in the code, so if anyone has any other suggestions I'd love to hear them.

推荐答案

我在这里发布解决方案,为未来的用户带来益处。我的合作伙伴花了好几个小时来琢磨出来。

I'm posting the solution here for the benefit of future users who run into this. It took many hours on the part of my partner to ferret this out.

解决方案的简单部分是编辑creation.py来更改

The simple part of the solution is to edit creation.py to change

def sql_for_inline_foreign_key_references (self, model, field, known_models, style):

to

def sql_for_inline_foreign_key_references (self, *args):

有一个例程在site-packages / django / db / backends / creation.py中具有相同的名称和几乎相同的语法,但是您需要在site-packages / django / db / backends / mysql / creation.py中。

There's a routine with the same name and almost the same syntax in site-packages/django/db/backends/creation.py, but you want the one in site-packages/django/db/backends/mysql/creation.py.

但是,还没有完成,因为模块被复制到site-packages / mysql / connector / django / creation.py,并且不会在编辑时自动发生,所以你需要手动复制它。我删除了.pyc文件的很好的措施,但不应该是必要的。

You're not done yet, though, because the module gets copied to site-packages/mysql/connector/django/creation.py, and that doesn't happen automatically on edit, so you'll need to copy it over manually. I deleted the .pyc file for good measure, but that "shouldn't" have been necessary.

当然,这不应该是必要的...

Of course, none of this should have been necessary...

这篇关于Django syncdb在SQLite中工作,在MySQL中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆