2014-02-23 47 views
0

当我期运用django.db.backends.postgresql_psycopg2和运行manage.py syncdb,以下错误引发Django的postgress:多个主键不允许

django.db.utils.ProgrammingError: multiple primary keys for table "token_place" are not allowed 
LINE 3:  "signatureid" integer NOT NULL PRIMARY KEY REFERENCES "s... 

型号:

class TokenPlace(models.Model): 
    token = models.ForeignKey(Token, db_column='tokenid', primary_key=True) 
    signature = models.ForeignKey(Signature, db_column='signatureid', primary_key=True) 
    place = models.IntegerField(primary_key=True) 

    class Meta: 
     db_table = 'token_place' 

我的模型正确地mysql但我必须工作部署它在postgres

如何解决这个问题?

+0

错误很明显不是吗?只要删除其中一个主键,它就会重新工作。是否有你想要多个主键的原因? – Wolph

+0

是的。数据库设计需要多个主键,我不能更改数据库设计。 – seyed

+1

检查您是否可以使用[surrogate key](http://en.wikipedia.org/wiki/Surrogate_key)并将'(token,signature)'设置为[unique together](https://docs.djangoproject)。 COM/EN /开发/ REF /模型/选项/#独特在一起)。 – danihp

回答

1

在阅读你的答案后,我终于明白了这个问题,你正在寻找Django中的复合键。不幸的是,这还不支持Django。如果你需要它有几个选项:

  1. 尝试Django的复合键的项目:https://github.com/simone/django-compositekey

  2. 使用此页上提供的补丁和解释的支持添加到Django的:https://code.djangoproject.com/wiki/MultipleColumnPrimaryKeys

  3. 使用SQLAlchemy(http://www.sqlalchemy.org/)进行查询,因为它确实支持此操作。

  4. 在Django中使用单个主键列,并从/中读/写一个可写视图,该视图与后台的实际表进行通信。