django数据库many2many关系错误

时间:2010-02-20 20:02:15

标签: python database django postgresql django-models

您好我正在使用以下模型来构建数据库

from django.db import models
from django.contrib import admin


class Team(models.Model):
    """Model docstring"""
    slug = models.SlugField(max_length=200)
    Team_ID = models.AutoField(primary_key=True)
    Team_Name = models.CharField(max_length=100,)
    College = models.CharField(max_length=100,)
    College = models.CharField(max_length=1,)
    Win = models.IntegerField()
    Loss  = models.IntegerField()
    Draw = models.IntegerField()

class Match(models.Model):

    Match_Id = models.AutoField(primary_key=True)
    Team_one = models.ManyToManyField('Team',related_name='Team one',symmetrical=False,) 
    Team_two = models.ManyToManyField('Team',related_name='Team two',symmetrical=False,) 
    stadium = models.CharField(max_length=255, blank=True)
    Start_time = models.DateTimeField(auto_now_add=False, auto_now=False, blank=True, null=True)
    Rafree = models.CharField(max_length=255, blank=True)
    Judge = models.CharField(max_length=255, blank=True)
    winner = models.ForeignKey('Team', related_name='winner',to_field='Team_Name')    
    updated = models.DateTimeField('update date', auto_now=True )
    created = models.DateTimeField('creation date', auto_now_add=True )

当我运行manage.py sqlall模型验证正常并给我sql输出如下

BEGIN;
CREATE TABLE "cupmanager_player" (
    "slug" varchar(200) NOT NULL,
    "Player_Id" serial NOT NULL PRIMARY KEY,
    "Player_Name" varchar(100) NOT NULL,
    "Nick" varchar(100) NOT NULL,
    "Jersy_Number" integer NOT NULL,
    "Team_id_id" integer NOT NULL,
    "Poistion" varchar(1) NOT NULL,
    "Red_card" integer NOT NULL,
    "Yellow_card" integer NOT NULL,
    "Points" integer NOT NULL
)
;
CREATE TABLE "cupmanager_team" (
    "slug" varchar(200) NOT NULL,
    "Team_ID" serial NOT NULL PRIMARY KEY,
    "Team_Name" varchar(100) NOT NULL,
    "College" varchar(1) NOT NULL,
    "Win" integer NOT NULL,
    "Loss" integer NOT NULL,
    "Draw" integer NOT NULL
)
;
ALTER TABLE "cupmanager_player" ADD CONSTRAINT "Team_id_id_refs_Team_ID_1a532b57"                 FOREIGN KEY ("Team_id_id") REFERENCES "cupmanager_team" ("Team_ID") DEFERRABLE INITIALLY     DEFERRED;
CREATE TABLE "cupmanager_match" (
    "Match_Id" serial NOT NULL PRIMARY KEY,
    "stadium" varchar(255) NOT NULL,
    "Start_time" timestamp with time zone,
    "Rafree" varchar(255) NOT NULL,
    "Judge" varchar(255) NOT NULL,
    "winner_id" varchar(100) NOT NULL REFERENCES "cupmanager_team" ("Team_Name")     DEFERRABLE INITIALLY DEFERRED,
    "updated" timestamp with time zone NOT NULL,
    "created" timestamp with time zone NOT NULL
)
;
CREATE TABLE "cupmanager_goal" (
    "Goal_ID" serial NOT NULL PRIMARY KEY,
    "Match_ID_id" integer NOT NULL REFERENCES "cupmanager_match" ("Match_Id")     DEFERRABLE INITIALLY DEFERRED,
    "Team_ID_id" varchar(100) NOT NULL REFERENCES "cupmanager_team" ("Team_Name") DEFERRABLE INITIALLY DEFERRED,
    "Player_ID_id" varchar(100) NOT NULL REFERENCES "cupmanager_player" ("Player_Name") DEFERRABLE INITIALLY DEFERRED,
    "name" varchar(100) NOT NULL,
    "updated" timestamp with time zone NOT NULL,
    "created" timestamp with time zone NOT NULL
)
;
CREATE TABLE "cupmanager_match_Team_one" (
    "id" serial NOT NULL PRIMARY KEY,
    "match_id" integer NOT NULL REFERENCES "cupmanager_match" ("Match_Id") DEFERRABLE INITIALLY DEFERRED,
    "team_id" integer NOT NULL REFERENCES "cupmanager_team" ("Team_ID") DEFERRABLE INITIALLY DEFERRED,
    UNIQUE ("match_id", "team_id")
)
;
CREATE TABLE "cupmanager_match_Team_two" (
    "id" serial NOT NULL PRIMARY KEY,
    "match_id" integer NOT NULL REFERENCES "cupmanager_match" ("Match_Id") DEFERRABLE INITIALLY DEFERRED,
    "team_id" integer NOT NULL REFERENCES "cupmanager_team" ("Team_ID") DEFERRABLE INITIALLY DEFERRED,
    UNIQUE ("match_id", "team_id")
)
;
CREATE INDEX "cupmanager_player_slug" ON "cupmanager_player" ("slug");
CREATE INDEX "cupmanager_player_Team_id_id" ON "cupmanager_player" ("Team_id_id");
CREATE INDEX "cupmanager_team_slug" ON "cupmanager_team" ("slug");
CREATE INDEX "cupmanager_match_winner_id" ON "cupmanager_match" ("winner_id");
CREATE INDEX "cupmanager_goal_Match_ID_id" ON "cupmanager_goal" ("Match_ID_id");
CREATE INDEX "cupmanager_goal_Team_ID_id" ON "cupmanager_goal" ("Team_ID_id");
CREATE INDEX "cupmanager_goal_Player_ID_id" ON "cupmanager_goal" ("Player_ID_id");
COMMIT;

但是当我现在运行manage.py syncdb命令时,我收到以下错误 psycopg2.ProgrammingError: there is no unique constraint matching given keys for referenced table "cupmanager_team"

知道这里发生了什么吗?我使用postgresql作为db后端,Postgresql_psycopg2作为db后端

1 个答案:

答案 0 :(得分:1)

你真的必须使用自己的主键字段吗?如果您未指定主键字段,则Django会自动创建名为id的字段。我没有看到命名字段的好处,例如match_id,特别是当您想稍后访问此字段时,它看起来像match.match_id 因此,如果没有自定义主键字段,它可能无效。

同样不是选项symmetrical只有在字段引用self时才会被考虑在内,即你自己没有在这里做的自己的表。