我打算使用django-compositekey连接到使用复合键的传统数据库。只是为了看到一切正常,我用这样一个简单的模型创建了一个新的Django项目。
from django.db import models
from compositekey import db
class Book(models.Model):
id = db.MultiFieldPK("author", "name")
name = models.CharField(max_length=100)
author = models.CharField(max_length=100)
如果我使用django_manage.py sql书籍一切似乎没问题,我收到了
BEGIN;
CREATE TABLE "Books_book" (
"name" varchar(100) NOT NULL,
"author" varchar(100) NOT NULL,
UNIQUE ("author", "name"),
PRIMARY KEY ("author", "name")
)
;
COMMIT;
但是,当我尝试使用syncdb时,收到以下错误
Creating tables ...
Traceback (most recent call last):
File "/home/islas/pycharm-3.4.1/helpers/pycharm/django_manage.py", line 23, in <module>
run_module(manage_file, None, '__main__', True)
File "/usr/lib/python2.7/runpy.py", line 176, in run_module
fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/home/islas/PycharmProjects/BooksDemo/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 415, in handle
return self.handle_noargs(**options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/syncdb.py", line 112, in handle_noargs
emit_post_sync_signal(created_models, verbosity, interactive, db)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/sql.py", line 216, in emit_post_sync_signal
interactive=interactive, db=db)
File "/usr/local/lib/python2.7/dist-packages/django/dispatch/dispatcher.py", line 185, in send
response = receiver(signal=self, sender=sender, **named)
File "/usr/local/lib/python2.7/dist- packages/django/contrib/auth/management/__init__.py", line 93, in create_permissions
"content_type", "codename"
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 538, in values_list
_fields=fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 852, in _clone
c._setup_query()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 995, in _setup_query
self.query.add_fields(self.field_names, True)
File "/usr/local/lib/python2.7/dist-packages/compositekey/db/models/sql/query.py", line 24, in add_fields
True)
ValueError: need more than 5 values to unpack
Process finished with exit code 1
有人知道可能出现什么问题吗?我是Django的新手,但据我所知,使用复合主键现在可以使用django-compositekey。
提前致谢,
亚历
答案 0 :(得分:1)
您使用的是Django
的哪个版本?我遇到了同样的问题,因为我使用Djnago 1.5.8
并且compositekey
包不支持Djnago 1.5.8
对于复合主键,您可以使用SQLAlchemy而不是本机Django ORM。它可以与Django ORM无缝共存。