我在django中創建了一些模型來表示新聞文章,作者以及每篇文章的地理焦點。我需要文章和作者之間的多對多關係,以及文章和位置之間的一對多關係,每個位置可以有多個文章,但反之亦然。我已經試過各種方法,但每次我在Django運行遷移我收到以下錯誤:Django Postgres完整性錯誤
django.db.utils.ProgrammingError: multiple default values specified for column "id" of table "location"
生成表中的代碼如下:
from django.contrib.gis.db import models
class Article(models.Model):
article_id = models.AutoField(primary_key=True)
article_title = models.CharField(max_length=200, unique_for_date="pub_date")
pub_date = models.DateTimeField('date published')
article_summary = models.TextField()
title_id = models.CharField(max_length=200)
section_id = models.CharField(max_length=200)
def __unicode__(self):
return u'%s %s' % (self.article_title, self.pub_date)
class Meta:
db_table = 'article'
class Author(models.Model):
author_id = models.AutoField(primary_key=True)
articles = models.ManyToManyField(Article)
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
def __unicode__(self):
return u'%s %s %s' % (self.articles, self.first_name, self.last_name)
class Meta:
db_table = 'author'
class Location(models.Model):
location_id = models.AutoField(primary_key=True)
lat = models.FloatField()
lon = models.FloatField()
local = models.CharField(max_length=200)
city = models.CharField(max_length=200)
region = models.CharField(max_length=200)
country = models.CharField(max_length=200)
continent = models.CharField(max_length=200)
article = models.ForeignKey(Article, default=0)
def __unicode__(self):
return u'%s %s %s' % (self.location_id, self.lat, self.lon)
class Meta:
db_table = 'location'
我想這件事情相對簡單,但它在過去的幾天裏逃脫了我。讓我知道你是否需要更多信息。
這是我現在用的,但上述同樣的問題仍然有機型代碼:
from django.contrib.gis.db import models
class Author(models.Model):
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name)
class Meta:
db_table = 'author'
class Location(models.Model):
lat = models.FloatField()
lon = models.FloatField()
local = models.CharField(max_length=200)
city = models.CharField(max_length=200)
region = models.CharField(max_length=200)
country = models.CharField(max_length=200)
continent = models.CharField(max_length=200)
def __unicode__(self):
return u'%s %s' % (self.lat, self.lon)
class Meta:
db_table = 'location'
class Article(models.Model):
authors = models.ManyToManyField(Author)
location = models.ForeignKey(Location)
article_title = models.CharField(max_length=200, unique_for_date="pub_date")
pub_date = models.DateTimeField('date published')
article_summary = models.TextField()
title_id = models.CharField(max_length=200)
section_id = models.CharField(max_length=200)
def __unicode__(self):
return u'%s %s' % (self.article_title, self.pub_date)
class Meta:
db_table = 'article'
你爲什麼要分配location_id,article_id等? django自動爲每個模型分配ID –
但是'id'似乎存在於數據庫中。你是否運行數據庫遷移? –
我來自sql背景,不習慣不明確定義事情!儘管你的方式更好! – sammy88888888