hi i am using the following models to build a database
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 )
when i run the manage.py sqlall the model validate fine and gives me the sql output as follows
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;
but when i now run the manage.py syncdb command i get the following error
psycopg2.ProgrammingError: there is no unique constraint matching given keys for referenced table "cupmanager_team"
any idea what is going on here ? i am using postgresql as the db backend and Postgresql_psycopg2 as the db back end
Mmh do you really have to use your own primary key fields? If you don’t specify a primary key field, than Django automatically creates a field called
id. I don’t see a a benefit from naming the fields e.g.match_id, especially as you want to access this field later, it will look likematch.match_id.So maybe it works without your custom primary key fields.
Also not that the option
symmetricalis only taken into account if the field is referencing self, i.e. the own table, which you are not doing here.